*****************************************************************
* The Dodd-Frank Act and Conflict in Africa's Great Lake Region *
*****************************************************************

* Set working directory
cd "C:\Users\jeffrey.bloem\OneDrive - USDA\Dodd Frank\Dodd Frank\Replication files"

* Load Raw ACLED data
use "ACLED_All_Africa.dta", clear
split event_date, p("/")
egen month_year = concat( event_date2 event_date3) 

gen admin2_n = 1
gen civilians = 1 if event_type=="Violence against civilians"
gen battles = 1 if event_type=="Battle-Government regains territory" | ///
event_type=="Battle-No change of territory" | event_type=="Battle-Non-state actor overtakes territory" 
gen riots_protest = event_type =="Riots/Protests"
gen other = 1 if event_type=="Headquarters or base established" | ///
event_type=="Non-violent transfer of territory" | event_type=="Remote violence" | ///
event_type=="Strategic development"
collapse (sum) admin2_n civilians battles riots_protest other fatalities, by(event_date3 event_date2 country admin1 admin2)

save "admin2_conflict_all.dta", replace

* Merge with full list of geographies 
use "admin2_conflict_all.dta", clear

gen group=1 if country=="Uganda" | country=="Rwanda" | country=="Burundi" | ///
country=="Tanzania" |  | country=="Zambia" | ///
country=="Angola" | country=="Republic of Congo" | ///
country=="Central African Republic" | country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Somalia" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.

* clean
replace admin2="Maseru" if country=="Lesotho" & admin1=="Maseru" & admin2==""
replace admin2="Leribe" if country=="Lesotho" & admin1=="Leribe" & admin2==""
replace admin2="Berea" if country=="Lesotho" & admin1=="Berea" & admin2==""
replace admin2="Butha-Buthe" if country=="Lesotho" & admin1=="Butha-Buthe" & admin2==""
replace admin2="Mokhotlong" if country=="Lesotho" & admin1=="Mokhotlong" & admin2==""
replace admin2="Mafeteng" if country=="Lesotho" & admin1=="Mafeteng" & admin2==""

rename event_date3 (year)
rename event_date2 (month)
egen year_month = concat(year month)
drop if year<"2004"
destring year, replace
destring month, replace

egen panel = group(year_month)
su panel, meanonly

* Generate all files
	foreach i of num 1/`r(max)' {
	preserve
	keep if panel==`i'
	save "files\admin2_conflict_`i'.dta", replace
	restore
	}
*

* Merge each file with full list of geographies
import excel "Geographic files\sample geographies.xlsx", sheet("Sheet1") firstrow clear
save "sample_geographies.dta", replace
use "sample_geographies.dta", clear

	foreach i of num 1/156 {
	preserve
	merge 1:1 country admin1 admin2 using "files\admin2_conflict_`i'.dta"
	rename (admin2_n) conflict_count
	replace conflict_count=0 if conflict_count==.
	replace fatalities=0 if fatalities==.
	replace civilians=0 if civilians==.
	replace battles=0 if battles==.
	replace riots_protest=0 if riots_protest==.
	replace other=0 if other==.
	egen year_hold = max(year)
	egen month_hold = max(month)
	drop year month 
	rename (year_hold) year
	rename (month_hold) month
	save "files\admin2_conflict_`i'.dta", replace
	restore
	}
*

* Append all files together
clear
	foreach i of num 1/156 {
	append using "files\admin2_conflict_`i'.dta"
	}
*

save "conflict_panel.dta", replace

******************
* Trends Figure **
******************
use "conflict_panel.dta", clear

* DRC only
drop group
gen group=1 if country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Somalia" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.

gen post2010=1 if year>2010
replace post2010=0 if post2010==.
gen conflict_flag=1 if conflict_count>0
replace conflict_flag=0 if conflict_flag==.
gen civilians_flag=1 if civilians>0
replace civilians_flag=0 if civilians==0
gen battles_flag=1 if battles>0
replace battles_flag=0 if battles==0
gen riots_protest_flag=1 if riots_protest>0
replace riots_protest_flag=0 if riots_protest==0
gen other_flag=1 if other>0
replace other_flag=0 if other==0
gen deadly_conflict_flag=1 if fatalities>0
replace deadly_conflict_flag=0 if fatalities==0

collapse (mean) conflict_flag civilians_flag battles_flag riots_protest_flag other_flag ///
deadly_conflict_flag, by(year month group)

save "drc_trends.dta", replace

use "conflict_panel.dta", clear

* All covered countries 
drop group

gen group=1 if country=="Uganda" | country=="Rwanda" | country=="Burundi" | ///
country=="Tanzania" |  | country=="Zambia" | ///
country=="Angola" | country=="Republic of Congo" | ///
country=="Central African Republic" | country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Somalia" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.

gen post2010=1 if year>2010
replace post2010=0 if post2010==.
gen df_conflict_flag=1 if conflict_count>0
replace df_conflict_flag=0 if df_conflict_flag==.
gen df_civilians_flag=1 if civilians>0
replace df_civilians_flag=0 if civilians==0
gen df_battles_flag=1 if battles>0
replace df_battles_flag=0 if battles==0
gen df_riots_protest_flag=1 if riots_protest>0
replace df_riots_protest_flag=0 if riots_protest==0
gen df_other_flag=1 if other>0
replace df_other_flag=0 if other==0
gen df_deadly_conflict_flag=1 if fatalities>0
replace df_deadly_conflict_flag=0 if fatalities==0

collapse (mean) df_conflict_flag df_civilians_flag df_battles_flag df_riots_protest_flag ///
df_other_flag df_deadly_conflict_flag, by(year month group)

save "df_trends.dta", replace

merge 1:1 year month group using "drc_trends.dta"
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace
drop _merge
reshape wide df_conflict_flag df_civilians_flag df_battles_flag df_riots_protest_flag ///
df_other_flag df_deadly_conflict_flag conflict_flag civilians_flag battles_flag ///
riots_protest_flag other_flag deadly_conflict_flag, i(year_month) j(group)
tostring year_month, replace
encode year_month, generate(time)

line conflict_flag1 conflict_flag0 df_conflict_flag1 time, lpattern(solid dash solid) ///
scheme(s1mono) xline(79) ylabel(0(0.25)0.5, angle(horizontal)) title("A", position(10)) ///
xtitle("Year") ytitle("Probability") legend(label(1 "DRC")) legend(label(2 "Covered Countries")) ///
legend(label(3 "Non-Covered Countries")) xlabel(0 "2004" 13 "2005" 25 "2006" 37 "2007" ///
49 "2008" 61 "2009" 73 "2010" 85 "2011" 97 "2012" 109 "2013" 121 "2014" 133 "2015" ///
145 "2016", angle(vertical))
graph save "trend_all_conflict.gph", replace

line civilians_flag1 civilians_flag0 df_civilians_flag1 time, lpattern(solid dash solid) ///
scheme(s1mono) xline(79) ylabel(0(0.25)0.5, angle(horizontal)) title("B", position(10)) ///
xtitle("Year") ytitle("Probability") legend(label(1 "DRC")) legend(label(2 "Covered Countries")) ///
legend(label(3 "Non-Covered Countries")) xlabel(0 "2004" 13 "2005" 25 "2006" 37 "2007" ///
49 "2008" 61 "2009" 73 "2010" 85 "2011" 97 "2012" 109 "2013" 121 "2014" 133 "2015" ///
145 "2016", angle(vertical))
graph save "trend_civilians.gph", replace

line battles_flag1 battles_flag0 df_battles_flag1 time, lpattern(solid dash solid) ///
scheme(s1mono) xline(79) ylabel(0(0.25)0.5, angle(horizontal)) title("C", position(10)) ///
xtitle("Year") ytitle("Probability") legend(label(1 "DRC")) legend(label(2 "Covered Countries")) ///
legend(label(3 "Non-Covered Countries")) xlabel(0 "2004" 13 "2005" 25 "2006" 37 "2007" ///
49 "2008" 61 "2009" 73 "2010" 85 "2011" 97 "2012" 109 "2013" 121 "2014" 133 "2015" ///
145 "2016", angle(vertical))
graph save "trend_battles.gph", replace

line riots_protest_flag1 riots_protest_flag0 df_riots_protest_flag1 time, ///
lpattern(solid dash solid) scheme(s1mono) xline(79) ylabel(0(0.25)0.5, angle(horizontal)) ///
title("D", position(10)) xtitle("Year") ytitle("Probability") legend(label(1 "DRC")) ///
legend(label(2 "Covered Countries")) legend(label(3 "Non-Covered Countries")) ///
xlabel(0 "2004" 13 "2005" 25 "2006" 37 "2007" 49 "2008" 61 "2009" 73 "2010" 85 "2011" ///
97 "2012" 109 "2013" 121 "2014" 133 "2015" 145 "2016", angle(vertical))
graph save "trend_riots_protest.gph", replace

line deadly_conflict_flag1 deadly_conflict_flag0 df_deadly_conflict_flag1 time, lpattern(solid dash solid) ///
scheme(s1mono) xline(79) ylabel(0(0.25)0.5, angle(horizontal)) title("E", position(10)) ///
xtitle("Year") ytitle("Probability") legend(label(1 "DRC")) legend(label(2 "Covered Countries")) ///
legend(label(3 "Non-Covered Countries")) xlabel(0 "2004" 13 "2005" 25 "2006" 37 "2007" ///
49 "2008" 61 "2009" 73 "2010" 85 "2011" 97 "2012" 109 "2013" 121 "2014" 133 "2015" ///
145 "2016", angle(vertical))
graph save "trend_deadly.gph", replace

grc1leg trend_all_conflict.gph trend_civilians.gph trend_battles.gph ///
trend_riots_protest.gph trend_deadly.gph, scheme(s1mono) cols(2) ycommon

*************************************
** Diff-in-diff Regression Model 1 **
*************************************
* DRC only panel
use "conflict_panel.dta", clear

drop group

gen group=1 if country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.

drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace

gen post2010=1 if year_month>201007
replace post2010=0 if post2010==.
gen conflict_flag=1 if conflict_count>0
replace conflict_flag=0 if conflict_flag==.
gen civilians_flag=1 if civilians>0
replace civilians_flag=0 if civilians==0
gen battles_flag=1 if battles>0
replace battles_flag=0 if battles==0
gen riots_protest_flag=1 if riots_protest>0
replace riots_protest_flag=0 if riots_protest==0
gen other_flag=1 if other>0
replace other_flag=0 if other==0
gen deadly_conflict_flag=1 if fatalities>0
replace deadly_conflict_flag=0 if fatalities==0
gen did = group*post2010
encode country, generate(country_id)
egen country_admin1 = concat(country admin1)
encode country_admin1, generate(admin1_id)
egen country_admin1_2 = concat(country admin1 admin2)
encode country_admin1_2, generate(admin2_id)
set matsize 10000

* Specification (1) Panel A

reg conflict_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table1.tex" , keep(did) replace
reg civilians_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table1.tex" , keep(did)
reg battles_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table1.tex" , keep(did)
reg riots_protest_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table1.tex" , keep(did)
reg deadly_conflict_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table1.tex" , keep(did)

*** Robustness to time interactions with FEs

* Linear
reg conflict_flag did i.year_month i.admin1_id c.year_month##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_linear.tex" , keep(did) replace
reg civilians_flag did i.year_month i.admin1_id c.year_month##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_linear.tex" , keep(did)
reg battles_flag did i.year_month i.admin1_id c.year_month##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_linear.tex" , keep(did)
reg riots_protest_flag did i.year_month i.admin1_id c.year_month##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_linear.tex" , keep(did)
reg deadly_conflict_flag did i.year_month i.admin1_id c.year_month##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_linear.tex" , keep(did)

* Qudratic
gen year_month2 = year_month^2
reg conflict_flag did i.year_month i.admin1_id c.year_month2##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_quad.tex" , keep(did) replace
reg civilians_flag did i.year_month i.admin1_id c.year_month2##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_quad.tex" , keep(did)
reg battles_flag did i.year_month i.admin1_id c.year_month2##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_quad.tex" , keep(did)
reg riots_protest_flag did i.year_month i.admin1_id c.year_month2##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_quad.tex" , keep(did)
reg deadly_conflict_flag did i.year_month i.admin1_id c.year_month2##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_quad.tex" , keep(did)

* Cubic
gen year_month3 = year_month^3
reg conflict_flag did i.year_month i.admin1_id c.year_month3##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_cube.tex" , keep(did) replace
reg civilians_flag did i.year_month i.admin1_id c.year_month3##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_cube.tex" , keep(did)
reg battles_flag did i.year_month i.admin1_id c.year_month3##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_cube.tex" , keep(did)
reg riots_protest_flag did i.year_month i.admin1_id c.year_month3##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_cube.tex" , keep(did)
reg deadly_conflict_flag did i.year_month i.admin1_id c.year_month3##i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_cube.tex" , keep(did)

* Robustness to alternative DV definitions
gen conflict_flag_5=1 if conflict_count>5
replace conflict_flag_5=0 if conflict_flag_5==.
gen civilians_flag_5=1 if civilians>5
replace civilians_flag_5=0 if civilians_flag_5==.
gen battles_flag_5=1 if battles>5
replace battles_flag_5=0 if battles_flag_5==.
gen riots_protest_flag_5=1 if riots_protest>5
replace riots_protest_flag_5=0 if riots_protest_flag_5==.
gen other_flag_5=1 if other>5
replace other_flag_5=0 if other_flag_5==.
gen deadly_conflict_flag_5=1 if fatalities>5
replace deadly_conflict_flag_5=0 if deadly_conflict_flag_5==.

reg conflict_flag_5 did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a.tex" , keep(did) replace
reg civilians_flag_5 did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a.tex" , keep(did)
reg battles_flag_5 did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a.tex" , keep(did)
reg riots_protest_flag_5 did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a.tex" , keep(did)
reg deadly_conflict_flag_5 did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a.tex" , keep(did)

* Baseline means
sum conflict_flag_5 if post2010==0 & group==1
sum civilians_flag_5 if post2010==0 & group==1
sum battles_flag_5 if post2010==0 & group==1
sum riots_protest_flag_5 if post2010 & group==1
sum other_flag_5 if post2010==0 & group==1
sum deadly_conflict_flag_5 if post2010==0 & group==1

gen conflict_flag_10=1 if conflict_count>10
replace conflict_flag_10=0 if conflict_flag_10==.
gen civilians_flag_10=1 if civilians>10
replace civilians_flag_10=0 if civilians_flag_10==.
gen battles_flag_10=1 if battles>10
replace battles_flag_10=0 if battles_flag_10==.
gen riots_protest_flag_10=1 if riots_protest>5
replace riots_protest_flag_10=0 if riots_protest_flag_10==.
gen other_flag_10=1 if other>10
replace other_flag_10=0 if other_flag_10==.
gen deadly_conflict_flag_10=1 if fatalities>10
replace deadly_conflict_flag_10=0 if deadly_conflict_flag_10==.

reg conflict_flag_10 did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table1b.tex" , keep(did) replace
reg civilians_flag_10 did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table1b.tex" , keep(did)
reg battles_flag_10 did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table1b.tex" , keep(did)
reg riots_protest_flag_10 did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table1b.tex" , keep(did)
reg deadly_conflict_flag_10 did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table1b.tex" , keep(did)

* Baseline means
sum conflict_flag_10 if post2010==0 & group==1
sum civilians_flag_10 if post2010==0 & group==1
sum battles_flag_10 if post2010==0 & group==1
sum riots_protest_flag_10 if post2010==0 & group==1
sum other_flag_10 if post2010==0 & group==1
sum deadly_conflict_flag_10 if post2010==0 & group==1

* Summary Statistics for DRC
tostring year_month, replace
encode year_month, gen(time_id)
mean conflict_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum conflict_flag if post2010==0 & group==1
reg conflict_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean civilians_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum civilians_flag if post2010==0 & group==1
reg civilians_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean battles_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum battles_flag if post2010==0 & group==1
reg battles_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean riots_protest_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum riots_protest_flag if post2010==0 & group==1
reg riots_protest_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean other_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum other_flag if post2010==0 & group==1
reg other_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean deadly_conflict_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum deadly_conflict_flag if post2010==0 & group==1
reg deadly_conflict_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)

* Summary Statistics for non-covered countries
mean conflict_flag if post2010==0 & group==0, vce(cluster admin2_id)
sum conflict_flag if post2010==0 & group==0
reg conflict_flag time_id if post2010==0 & group==0, vce(cluster admin2_id)
mean civilians_flag if post2010==0 & group==0, vce(cluster admin2_id)
sum civilians_flag if post2010==0 & group==0
reg civilians_flag time_id if post2010==0 & group==0, vce(cluster admin2_id)
mean battles_flag if post2010==0 & group==0, vce(cluster admin2_id)
sum battles_flag if post2010==0 & group==0
reg battles_flag time_id if post2010==0 & group==0, vce(cluster admin2_id)
mean riots_protest_flag if post2010==0 & group==0, vce(cluster admin2_id)
sum riots_protest_flag if post2010==0 & group==0
reg riots_protest_flag time_id if post2010==0 & group==0, vce(cluster admin2_id)
mean other_flag if post2010==0 & group==0, vce(cluster admin2_id)
sum other_flag if post2010==0 & group==0
reg other_flag time_id if post2010==0 & group==0, vce(cluster admin2_id)
mean deadly_conflict_flag if post2010==0 & group==0, vce(cluster admin2_id)
sum deadly_conflict_flag if post2010==0 & group==0
reg deadly_conflict_flag time_id if post2010==0 & group==0, vce(cluster admin2_id)

*** Robustness check: DRC compared to other covered countries
use "conflict_panel.dta", clear

drop group

gen group=0 if country=="Uganda" | country=="Rwanda" | country=="Burundi" | ///
country=="Tanzania" |  | country=="Zambia" | ///
country=="Angola" | country=="Republic of Congo" | ///
country=="Central African Republic" 
replace group=1 if country=="Democratic Republic of Congo"
drop if group==.

drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace

gen post2010=1 if year_month>201007
replace post2010=0 if post2010==.
gen conflict_flag=1 if conflict_count>0
replace conflict_flag=0 if conflict_flag==.
gen civilians_flag=1 if civilians>0
replace civilians_flag=0 if civilians==0
gen battles_flag=1 if battles>0
replace battles_flag=0 if battles==0
gen riots_protest_flag=1 if riots_protest>0
replace riots_protest_flag=0 if riots_protest==0
gen other_flag=1 if other>0
replace other_flag=0 if other==0
gen deadly_conflict_flag=1 if fatalities>0
replace deadly_conflict_flag=0 if fatalities==0
gen did = group*post2010
encode country, generate(country_id)
egen country_admin1 = concat(country admin1)
encode country_admin1, generate(admin1_id)
egen country_admin1_2 = concat(country admin1 admin2)
encode country_admin1_2, generate(admin2_id)
set matsize 10000

* Specification (1) DRC vs. other *covered* countries

reg conflict_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table1a_other.tex" , keep(did) replace
reg civilians_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table1a_other.tex" , keep(did)
reg battles_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table1a_other.tex" , keep(did)
reg riots_protest_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table1a_other.tex" , keep(did)
reg deadly_conflict_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table1a_other.tex" , keep(did)

* Placebo tests

* A: All conflict
use "conflict_panel.dta", clear
drop if country=="Uganda" | country=="Rwanda" | country=="Burundi" | ///
country=="Tanzania" |  | country=="Zambia" | country=="Angola" | ///
country=="Republic of Congo" | country=="Central African Republic" | country=="Somalia"
save "conflict_panel_placebo.dta", replace
collapse (mean) group, by(country)
drop group
gen effect = .
egen country_num = group(country)
su country_num, meanonly
foreach i of num 1/`r(max)' {
	preserve
	
	use "conflict_panel_placebo.dta", clear
	egen country_num = group(country)
	
	drop group
	gen group=1 if country_num==`i'
	replace group=0 if country_num!=`i'
	drop if group==.

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen post2010=1 if year_month>201007
	replace post2010=0 if post2010==.
	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_flag==.
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0
	gen did = group*post2010
	encode country, generate(country_id)
	egen country_admin1 = concat(country admin1)
	encode country_admin1, generate(admin1_id)
	egen country_admin1_2 = concat(country admin1 admin2)
	encode country_admin1_2, generate(admin2_id)
	set matsize 10000

	reg conflict_flag did i.year_month i.admin1_id, vce(cluster admin2_id)
	
	local effect = _b[did]
	
	restore
	
	replace effect = `effect' in `i'
}
*
save "placebo_all_conflict.dta", replace
use "placebo_all_conflict.dta", clear
drop if country=="Democratic Republic of Congo"
histogram effect , freq bin(31) scheme(s1mono) xline(0.143) lcolor(black) ///
xline(-0.042, lpattern(dash)) xline(0.079, lpattern(dash)) title("A", position(10)) ///
xtitle("Placebo Estimates") ylabel(, angle(horizontal))
graph save "drc_all_conflict.gph", replace

* B: Civilians
use "conflict_panel.dta", clear
drop if country=="Uganda" | country=="Rwanda" | country=="Burundi" | ///
country=="Tanzania" |  | country=="Zambia" | country=="Angola" | ///
country=="Republic of Congo" | country=="Central African Republic" | country=="Somalia"
save "conflict_panel_placebo.dta", replace
collapse (mean) group, by(country)
drop group
gen effect = .
egen country_num = group(country)
su country_num, meanonly
foreach i of num 1/`r(max)' {
	preserve
	
	use "conflict_panel_placebo.dta", clear
	egen country_num = group(country)
	
	drop group
	gen group=1 if country_num==`i'
	replace group=0 if country_num!=`i'
	drop if group==.

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen post2010=1 if year_month>201007
	replace post2010=0 if post2010==.
	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_flag==.
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0
	gen did = group*post2010
	encode country, generate(country_id)
	egen country_admin1 = concat(country admin1)
	encode country_admin1, generate(admin1_id)
	egen country_admin1_2 = concat(country admin1 admin2)
	encode country_admin1_2, generate(admin2_id)
	set matsize 10000

	reg civilians_flag did i.year_month i.admin1_id, vce(cluster admin2_id)
	
	local effect = _b[did]
	
	restore
	
	replace effect = `effect' in `i'
}
*
save "placebo_civilians.dta", replace
use "placebo_civilians.dta", clear
drop if country=="Democratic Republic of Congo"
histogram effect , freq bin(31) scheme(s1mono) xline(0.076) lcolor(black) ///
xline(-0.029, lpattern(dash)) xline(0.026, lpattern(dash))title("B", position(10)) ///
xtitle("Placebo Estimates") ylabel(, angle(horizontal))
graph save "drc_civilians.gph", replace

* C: Battles
use "conflict_panel.dta", clear
drop if country=="Uganda" | country=="Rwanda" | country=="Burundi" | ///
country=="Tanzania" |  | country=="Zambia" | country=="Angola" | ///
country=="Republic of Congo" | country=="Central African Republic" | country=="Somalia"
save "conflict_panel_placebo.dta", replace
collapse (mean) group, by(country)
drop group
gen effect = .
egen country_num = group(country)
su country_num, meanonly
foreach i of num 1/`r(max)' {
	preserve
	
	use "conflict_panel_placebo.dta", clear
	egen country_num = group(country)
	
	drop group
	gen group=1 if country_num==`i'
	replace group=0 if country_num!=`i'
	drop if group==.

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen post2010=1 if year_month>201007
	replace post2010=0 if post2010==.
	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_flag==.
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0
	gen did = group*post2010
	encode country, generate(country_id)
	egen country_admin1 = concat(country admin1)
	encode country_admin1, generate(admin1_id)
	egen country_admin1_2 = concat(country admin1 admin2)
	encode country_admin1_2, generate(admin2_id)
	set matsize 10000

	reg battles_flag did i.year_month i.admin1_id, vce(cluster admin2_id)
	
	local effect = _b[did]
	
	restore
	
	replace effect = `effect' in `i'
}
*
save "placebo_battles.dta", replace
use "placebo_battles.dta", clear
drop if country=="Democratic Republic of Congo"
histogram effect , freq bin(31) scheme(s1mono) xline(0.063) lcolor(black) ///
xline(-0.010, lpattern(dash)) xline(0.015, lpattern(dash)) title("C", position(10)) ///
xtitle("Placebo Estimates") ylabel(, angle(horizontal)) ///
xscale(range(-0.04 -0.02 0 0.02 0.06 0.06 0.07))
graph save "drc_battles.gph", replace

* D: Riots and Protests
use "conflict_panel.dta", clear
drop if country=="Uganda" | country=="Rwanda" | country=="Burundi" | ///
country=="Tanzania" |  | country=="Zambia" | country=="Angola" | ///
country=="Republic of Congo" | country=="Central African Republic" | country=="Somalia"
save "conflict_panel_placebo.dta", replace
collapse (mean) group, by(country)
drop group
gen effect = .
egen country_num = group(country)
su country_num, meanonly
foreach i of num 1/`r(max)' {
	preserve
	
	use "conflict_panel_placebo.dta", clear
	egen country_num = group(country)
	
	drop group
	gen group=1 if country_num==`i'
	replace group=0 if country_num!=`i'
	drop if group==.

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen post2010=1 if year_month>201007
	replace post2010=0 if post2010==.
	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_flag==.
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen riots_protest_flag=1 if riots_protest>0
	replace riots_protest_flag=0 if riots_protest==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0
	gen did = group*post2010
	encode country, generate(country_id)
	egen country_admin1 = concat(country admin1)
	encode country_admin1, generate(admin1_id)
	egen country_admin1_2 = concat(country admin1 admin2)
	encode country_admin1_2, generate(admin2_id)
	set matsize 10000

	reg riots_protest_flag did i.year_month i.admin1_id, vce(cluster admin2_id)
	
	local effect = _b[did]
	
	restore
	
	replace effect = `effect' in `i'
}
*
save "placebo_riots_protest.dta", replace
use "placebo_riots_protest.dta", clear
drop if country=="Democratic Republic of Congo"
histogram effect , freq bin(31) scheme(s1mono) xline(0.102) lcolor(black) ///
xline(-0.024, lpattern(dash)) xline(0.035, lpattern(dash)) title("D", position(10)) ///
xtitle("Placebo Estimates") ylabel(, angle(horizontal))
graph save "drc_riots_protest.gph", replace

* E: Deadly Conflict
use "conflict_panel.dta", clear
drop if country=="Uganda" | country=="Rwanda" | country=="Burundi" | ///
country=="Tanzania" |  | country=="Zambia" | country=="Angola" | ///
country=="Republic of Congo" | country=="Central African Republic" | country=="Somalia"
save "conflict_panel_placebo.dta", replace
collapse (mean) group, by(country)
drop group
gen effect = .
egen country_num = group(country)
su country_num, meanonly
foreach i of num 1/`r(max)' {
	preserve
	
	use "conflict_panel_placebo.dta", clear
	egen country_num = group(country)
	
	drop group
	gen group=1 if country_num==`i'
	replace group=0 if country_num!=`i'
	drop if group==.

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen post2010=1 if year_month>201007
	replace post2010=0 if post2010==.
	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_flag==.
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0
	gen did = group*post2010
	encode country, generate(country_id)
	egen country_admin1 = concat(country admin1)
	encode country_admin1, generate(admin1_id)
	egen country_admin1_2 = concat(country admin1 admin2)
	encode country_admin1_2, generate(admin2_id)
	set matsize 10000

	reg deadly_conflict_flag did i.year_month i.admin1_id, vce(cluster admin2_id)
	
	local effect = _b[did]
	
	restore
	
	replace effect = `effect' in `i'
}
*
save "placebo_deadly.dta", replace
use "placebo_deadly.dta", clear
drop if country=="Democratic Republic of Congo"
histogram effect , freq bin(31) scheme(s1mono) xline(0.0682) lcolor(black) ///
xline(-0.019, lpattern(dash)) xline(0.051, lpattern(dash)) title("E", position(10)) xtitle("Placebo Estimates") ylabel(, angle(horizontal))
graph save "drc_deadly.gph", replace

gr combine drc_all_conflict.gph drc_civilians.gph drc_battles.gph drc_riots_protest.gph ///
drc_deadly.gph, scheme(s1mono) cols(2)


* All Dodd Frank countries panel
use "conflict_panel.dta", clear

drop group

gen group=1 if country=="Uganda" | country=="Rwanda" | country=="Burundi" | ///
country=="Tanzania" |  | country=="Zambia" | ///
country=="Angola" | country=="Republic of Congo" | ///
country=="Central African Republic" | country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.

drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace

gen post2010=1 if year_month>201007
replace post2010=0 if post2010==.
gen conflict_flag=1 if conflict_count>0
replace conflict_flag=0 if conflict_flag==.
gen civilians_flag=1 if civilians>0
replace civilians_flag=0 if civilians==0
gen battles_flag=1 if battles>0
replace battles_flag=0 if battles==0
gen riots_protest_flag=1 if riots_protest>0
replace riots_protest_flag=0 if riots_protest==0
gen other_flag=1 if other>0
replace other_flag=0 if other==0
gen deadly_conflict_flag=1 if fatalities>0
replace deadly_conflict_flag=0 if fatalities==0
gen did = group*post2010
encode country, generate(country_id)
egen country_admin1 = concat(country admin1)
encode country_admin1, generate(admin1_id)
egen country_admin1_2 = concat(country admin1 admin2)
encode country_admin1_2, generate(admin2_id)
set matsize 10000

* Specification (1) Panel B

reg conflict_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table2.tex" , keep(did) replace
reg civilians_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table2.tex" , keep(did)
reg battles_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table2.tex" , keep(did)
reg riots_protest_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table2.tex" , keep(did)
reg deadly_conflict_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table2.tex" , keep(did)

* Summary Statistics for all covered countries
tostring year_month, replace
encode year_month, gen(time_id)
mean conflict_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum conflict_flag if post2010==0 & group==1
reg conflict_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean civilians_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum civilians_flag if post2010==0 & group==1
reg civilians_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean battles_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum battles_flag if post2010==0 & group==1
reg battles_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean riots_protest_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum riots_protest_flag if post2010==0 & group==1
reg riots_protest_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean other_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum other_flag if post2010==0 & group==1
reg other_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean deadly_conflict_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum deadly_conflict_flag if post2010==0 & group==1
reg deadly_conflict_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)

sum conflict_count

* Surrounding countries only (all covered countries minus DRC) panel
use "conflict_panel.dta", clear

drop group

gen group=1 if country=="Uganda" | country=="Rwanda" | country=="Burundi" | ///
country=="Tanzania" |  | country=="Zambia" | ///
country=="Angola" | country=="Republic of Congo" | ///
country=="Central African Republic" 
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.

drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace

gen post2010=1 if year_month>201007
replace post2010=0 if post2010==.
gen conflict_flag=1 if conflict_count>0
replace conflict_flag=0 if conflict_flag==.
gen civilians_flag=1 if civilians>0
replace civilians_flag=0 if civilians==0
gen battles_flag=1 if battles>0
replace battles_flag=0 if battles==0
gen riots_protest_flag=1 if riots_protest>0
replace riots_protest_flag=0 if riots_protest==0
gen other_flag=1 if other>0
replace other_flag=0 if other==0
gen deadly_conflict_flag=1 if fatalities>0
replace deadly_conflict_flag=0 if fatalities==0
gen did = group*post2010
encode country, generate(country_id)
egen country_admin1 = concat(country admin1)
encode country_admin1, generate(admin1_id)
egen country_admin1_2 = concat(country admin1 admin2)
encode country_admin1_2, generate(admin2_id)
set matsize 10000

* Specification (1) appendix table

reg conflict_flag did i.year_month i.admin1_id, vce(cluster country_id)

reg civilians_flag did i.year_month i.admin1_id, vce(cluster country_id)

reg battles_flag did i.year_month i.admin1_id, vce(cluster country_id)

reg riots_protest_flag did i.year_month i.admin1_id, vce(cluster country_id)

reg deadly_conflict_flag did i.year_month i.admin1_id, vce(cluster country_id)


* Summary Statistics for all covered countries
tostring year_month, replace
encode year_month, gen(time_id)
mean conflict_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum conflict_flag if post2010==0 & group==1
reg conflict_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean civilians_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum civilians_flag if post2010==0 & group==1
reg civilians_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean battles_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum battles_flag if post2010==0 & group==1
reg battles_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean riots_protest_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum riots_protest_flag if post2010==0 & group==1
reg riots_protest_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)

mean other_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum other_flag if post2010==0 & group==1
reg other_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean deadly_conflict_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum deadly_conflict_flag if post2010==0 & group==1
reg deadly_conflict_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)

*** Each country covered by Dodd Frank individually
use "conflict_panel.dta", clear
encode country, generate(country_id)

local countrylist 37 29 5 35 38 1 28 7

foreach i of local countrylist {
	preserve
	
	use "conflict_panel.dta", clear
	
	encode country, generate(country_id)
	
	drop group
	gen group=1 if country_id==`i'
	replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
	country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
	country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
	country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
	country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
	country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
	country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
	country=="Swaziland" | country=="Niger" | country=="Mali"
	drop if group==.
	
	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen post2010=1 if year_month>201007
	replace post2010=0 if post2010==.
	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_flag==.
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen riots_protest_flag=1 if riots_protest>0
	replace riots_protest_flag=0 if riots_protest==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0
	gen did = group*post2010
	egen country_admin1 = concat(country admin1)
	encode country_admin1, generate(admin1_id)
	egen country_admin1_2 = concat(country admin1 admin2)
	encode country_admin1_2, generate(admin2_id)
	set matsize 10000

	reg conflict_flag did i.year_month i.admin1_id, vce(cluster country_id)
	outreg2 using "Table3_`i'.tex" , keep(did) replace
	*reg civilians_flag did i.year_month i.admin1_id, vce(cluster country_id)
	*outreg2 using "C:\Users\JEFFREY.BLOEM\Documents\Dodd Frank\Dodd Frank\Table3_`i'.tex" , keep(did)
	*reg battles_flag did i.year_month i.admin1_id, vce(cluster country_id)
	*outreg2 using "C:\Users\JEFFREY.BLOEM\Documents\Dodd Frank\Dodd Frank\Table3_`i'.tex" , keep(did)
	reg riots_protest_flag did i.year_month i.admin1_id, vce(cluster country_id)
	outreg2 using "Table3_`i'.tex" , keep(did)
	*reg deadly_conflict_flag i.year_month i.admin1_id did, vce(cluster country_id)
	*outreg2 using "C:\Users\JEFFREY.BLOEM\Documents\Dodd Frank\Dodd Frank\Table3_`i'.tex" , keep(did)

	restore
}
*

*******************************************************
** Spillover analysis **
*******************************************************

** Only including Eastern DRC
use "conflict_panel.dta", clear

drop group

gen group=1 if country=="Democratic Republic of Congo" & admin1=="Sud-Kivu"
replace group=1 if country=="Democratic Republic of Congo" & admin1=="Orientale"
replace group=1 if country=="Democratic Republic of Congo" & admin1=="Nord-Kivu"
replace group=1 if country=="Democratic Republic of Congo" & admin1=="Maniema"
replace group=1 if country=="Democratic Republic of Congo" & admin1=="Katanga"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.

drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace

gen post2010=1 if year_month>201007
replace post2010=0 if post2010==.
gen conflict_flag=1 if conflict_count>0
replace conflict_flag=0 if conflict_flag==.
gen civilians_flag=1 if civilians>0
replace civilians_flag=0 if civilians==0
gen battles_flag=1 if battles>0
replace battles_flag=0 if battles==0
gen riots_protest_flag=1 if riots_protest>0
replace riots_protest_flag=0 if riots_protest==0
gen other_flag=1 if other>0
replace other_flag=0 if other==0
gen deadly_conflict_flag=1 if fatalities>0
replace deadly_conflict_flag=0 if fatalities==0
gen did = group*post2010
encode country, generate(country_id)
egen country_admin1 = concat(country admin1)
encode country_admin1, generate(admin1_id)
egen country_admin1_2 = concat(country admin1 admin2)
encode country_admin1_2, generate(admin2_id)
set matsize 10000

* Specification (1) Panel A

reg conflict_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "Table4.tex" , keep(did) replace
reg civilians_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table4.tex" , keep(did)
reg battles_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table4.tex" , keep(did)
reg riots_protest_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table4.tex" , keep(did)
reg deadly_conflict_flag did i.year_month i.admin1_id, vce(cluster country_id)
outreg2 using "Table4.tex" , keep(did)

* Summary Statistics for Eastern DRC provinces
tostring year_month, replace
encode year_month, gen(time_id)
mean conflict_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum conflict_flag if post2010==0 & group==1
reg conflict_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean civilians_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum civilians_flag if post2010==0 & group==1
reg civilians_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean battles_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum battles_flag if post2010==0 & group==1
reg battles_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean riots_protest_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum riots_protest_flag if post2010==0 & group==1
reg riots_protest_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean other_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum other_flag if post2010==0 & group==1
reg other_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)
mean deadly_conflict_flag if post2010==0 & group==1, vce(cluster admin2_id)
sum deadly_conflict_flag if post2010==0 & group==1
reg deadly_conflict_flag time_id if post2010==0 & group==1, vce(cluster admin2_id)

** Spillover patterns
use "conflict_panel.dta", clear

* Define mining territories (based on IPIS data)
drop group

gen group=1 if country=="Uganda" | country=="Rwanda" | country=="Burundi" | ///
country=="Tanzania" |  | country=="Zambia" | ///
country=="Angola" | country=="Republic of Congo" | ///
country=="Central African Republic" | country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.

gen mining_ter = .
replace mining_ter = 1 if country=="Democratic Republic of Congo" & admin1=="Orientale" & admin2=="Ituri"
replace mining_ter = 1 if country=="Democratic Republic of Congo" & admin1=="Orientale" & admin2=="Tshopo"
replace mining_ter = 1 if country=="Democratic Republic of Congo" & admin1=="Maniema" & admin2=="Maniema"
replace mining_ter = 1 if country=="Democratic Republic of Congo" & admin1=="Nord-Kivu" & admin2=="Nord-Kivu"
replace mining_ter = 1 if country=="Democratic Republic of Congo" & admin1=="Sud-Kivu" & admin2=="Sud-Kivu"
replace mining_ter = 0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"

* Define eastern DRC
gen eastern_drc = .
replace eastern_drc = 1 if country=="Democratic Republic of Congo" & admin1=="Sud-Kivu"
replace eastern_drc = 1 if country=="Democratic Republic of Congo" & admin1=="Orientale"
replace eastern_drc = 1 if country=="Democratic Republic of Congo" & admin1=="Nord-Kivu"
replace eastern_drc = 1 if country=="Democratic Republic of Congo" & admin1=="Maniema"
replace eastern_drc = 1 if country=="Democratic Republic of Congo" & admin1=="Katanga"
replace eastern_drc = 0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"

* Define non-mining territories in the eastern DRC
gen nonmining_ter = 1 if eastern_drc==1 & mining_ter==.
replace nonmining_ter = 0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"

* Define Western DRC
gen western_drc = 1 if eastern_drc==.
replace western_drc = 0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"

* Regression Analysis
drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace

gen post2010=1 if year_month>201007
replace post2010=0 if post2010==.
gen conflict_flag=1 if conflict_count>0
replace conflict_flag=0 if conflict_flag==.
gen civilians_flag=1 if civilians>0
replace civilians_flag=0 if civilians==0
gen battles_flag=1 if battles>0
replace battles_flag=0 if battles==0
gen riots_protest_flag=1 if riots_protest>0
replace riots_protest_flag=0 if riots_protest==0
gen other_flag=1 if other>0
replace other_flag=0 if other==0
gen deadly_conflict_flag=1 if fatalities>0
replace deadly_conflict_flag=0 if fatalities==0

gen did_mining = mining_ter*post2010
gen did_nonmining = nonmining_ter*post2010
gen did_western = western_drc*post2010
gen did_eastern = eastern_drc*post2010

encode country, generate(country_id)
egen country_admin1 = concat(country admin1)
encode country_admin1, generate(admin1_id)
egen country_admin1_2 = concat(country admin1 admin2)
encode country_admin1_2, generate(admin2_id)
set matsize 10000


reg conflict_flag did_mining i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1b.tex" , keep(did_mining) replace
reg civilians_flag did_mining i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1b.tex" , keep(did_mining)
reg battles_flag did_mining i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1b.tex" , keep(did_mining)
reg riots_protest_flag did_mining i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1b.tex" , keep(did_mining)
reg deadly_conflict_flag did_mining i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1b.tex" , keep(did_mining)

mean conflict_flag if post2010==0 & mining_ter==1
mean civilians_flag if post2010==0 & mining_ter==1
mean battles_flag if post2010==0 & mining_ter==1
mean riots_protest_flag if post2010==0 & mining_ter==1
mean deadly_conflict_flag if post2010==0 & mining_ter==1

reg conflict_flag did_nonmining i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1c.tex" , keep(did_nonmining) replace
reg civilians_flag did_nonmining i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1c.tex" , keep(did_nonmining)
reg battles_flag did_nonmining i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1c.tex" , keep(did_nonmining)
reg riots_protest_flag did_nonmining i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1c.tex" , keep(did_nonmining)
reg deadly_conflict_flag did_nonmining i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1c.tex" , keep(did_nonmining)

mean conflict_flag if post2010==0 & nonmining_ter==1
mean civilians_flag if post2010==0 & nonmining_ter==1
mean battles_flag if post2010==0 & nonmining_ter==1
mean riots_protest_flag if post2010==0 & nonmining_ter==1
mean deadly_conflict_flag if post2010==0 & nonmining_ter==1

reg conflict_flag did_western i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1d.tex" , keep(did_western) replace
reg civilians_flag did_western i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1d.tex" , keep(did_western)
reg battles_flag did_western i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1d.tex" , keep(did_western)
reg riots_protest_flag did_western i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1d.tex" , keep(did_western)
reg deadly_conflict_flag did_western i.year_month i.admin1_id, vce(cluster country_id)
outreg2  using "TableA1d.tex" , keep(did_western)

mean conflict_flag if post2010==0 & western_drc==1
mean civilians_flag if post2010==0 & western_drc==1
mean battles_flag if post2010==0 & western_drc==1
mean riots_protest_flag if post2010==0 & western_drc==1
mean deadly_conflict_flag if post2010==0 & western_drc==1

* Make Figure
drop year_month
gen month_str = string(month, "%02.0f")
egen year_month = concat(year month_str)

collapse (sum) conflict_count, by (year_month mining_ter nonmining_ter western_drc)

encode year_month, gen(time_id)

twoway (line conflict_count time_id if mining_ter==1, lpattern(solid)) ///
(line conflict_count time_id if nonmining_ter==1, lpattern(solid)) ///
(line conflict_count time_id if western_drc==1, lpattern(dash)), ///
scheme(s1mono) xline(79) ylabel(, angle(horizontal)) xtitle("Year") ///
ytitle("Conflict Event Count") legend(label(1 "Eastern DRC, 3T Mining")) ///
legend(label(2 "Eastern DRC, non-3T Mining")) ///
legend(label(3 "Western DRC")) xlabel(0 "2004" 13 "2005" 25 "2006" 37 "2007" ///
49 "2008" 61 "2009" 73 "2010" 85 "2011" 97 "2012" 109 "2013" 121 "2014" 133 "2015" ///
145 "2016", angle(vertical))

* Correlation analysis
gen group = 1 if mining_ter==1
replace group = 2 if nonmining_ter==1
replace group = 3 if western_drc==1
drop mining_ter nonmining_ter western_drc year_month

reshape wide conflict_count, i(time_id) j(group)

rename conflict_count1 (mining_ter)
rename conflict_count2 (nonmining_ter)
rename conflict_count3 (western_drc)

reg nonmining_ter mining_ter, robust
reg western_drc mining_ter, robust


*************************************
** Diff-in-diff Regression Model 2 **
*************************************
* DRC only 
use "conflict_panel.dta", clear

drop group

gen group=1 if country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.

drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace

gen post2010=1 if year_month>201007
replace post2010=0 if post2010==.
gen conflict_flag=1 if conflict_count>0
replace conflict_flag=0 if conflict_flag==.
gen civilians_flag=1 if civilians>0
replace civilians_flag=0 if civilians==0
gen battles_flag=1 if battles>0
replace battles_flag=0 if battles==0
gen riots_protest_flag=1 if riots_protest>0
replace riots_protest_flag=0 if riots_protest==0
gen other_flag=1 if other>0
replace other_flag=0 if other==0
gen deadly_conflict_flag=1 if fatalities>0
replace deadly_conflict_flag=0 if fatalities==0
gen did = group*post2010
encode country, generate(country_id)
egen country_admin1 = concat(country admin1)
encode country_admin1, generate(admin1_id)
egen country_admin1_2 = concat(country admin1 admin2)
encode country_admin1_2, generate(admin2_id)
set matsize 10000


* Specification (2) 
gen b2004 = 1 if year==2004 & group==1
replace b2004 = 0 if b2004==.
gen b2005 = 1 if year==2005 & group==1
replace b2005 = 0 if b2005==.
gen b2006 = 1 if year==2006 & group==1
replace b2006 = 0 if b2006==.
gen b2007 = 1 if year==2007 & group==1
replace b2007 = 0 if b2007==.
gen b2008 = 1 if year==2008 & group==1
replace b2008 = 0 if b2008==.
gen b2009 = 1 if year==2009 & group==1
replace b2009 = 0 if b2009==.
gen b2010 = 1 if year==2010 & group==1
replace b2010 = 0 if b2010==.
gen b2011 = 1 if year==2011 & group==1
replace b2011 = 0 if b2011==.
gen b2012 = 1 if year==2012 & group==1
replace b2012 = 0 if b2012==.
gen b2013 = 1 if year==2013 & group==1
replace b2013 = 0 if b2013==.
gen b2014 = 1 if year==2014 & group==1
replace b2014 = 0 if b2014==.
gen b2015 = 1 if year==2015 & group==1
replace b2015 = 0 if b2015==.
gen b2016 = 1 if year==2016 & group==1
replace b2016 = 0 if b2016==.

reg conflict_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)

coefplot, vertical keep(b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016) yline(0) scheme(s1mono) rename (b2005 = 2005 b2006 = 2006 ///
b2007 = 2007 b2008 = 2008 b2009 = 2009 b2010 = 2010 b2011 = 2011 b2012 = 2012 ///
b2013 = 2013 b2014 = 2014 b2015 = 2015 b2016 = 2016) xline(6.5, lpattern(dash)) ///
title("A", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical))
graph save "drc_model2_all_conflict.gph", replace

reg civilians_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)

coefplot, vertical keep(b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016) yline(0) scheme(s1mono) rename (b2005 = 2005 b2006 = 2006 ///
b2007 = 2007 b2008 = 2008 b2009 = 2009 b2010 = 2010 b2011 = 2011 b2012 = 2012 ///
b2013 = 2013 b2014 = 2014 b2015 = 2015 b2016 = 2016) xline(6.5, lpattern(dash)) ///
title("B", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical))
graph save "drc_model2_civilians.gph", replace

reg battles_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)

coefplot, vertical keep(b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016) yline(0) scheme(s1mono) rename (b2005 = 2005 b2006 = 2006 ///
b2007 = 2007 b2008 = 2008 b2009 = 2009 b2010 = 2010 b2011 = 2011 b2012 = 2012 ///
b2013 = 2013 b2014 = 2014 b2015 = 2015 b2016 = 2016) xline(6.5, lpattern(dash)) ///
title("C", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical))
graph save "drc_model2_battles.gph", replace

reg riots_protest_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)

coefplot, vertical keep(b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016) yline(0) scheme(s1mono) rename (b2005 = 2005 b2006 = 2006 ///
b2007 = 2007 b2008 = 2008 b2009 = 2009 b2010 = 2010 b2011 = 2011 b2012 = 2012 ///
b2013 = 2013 b2014 = 2014 b2015 = 2015 b2016 = 2016) xline(6.5, lpattern(dash)) ///
title("D", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical))
graph save "drc_model2_riots_protest.gph", replace

reg deadly_conflict_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)

coefplot, vertical keep(b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016) yline(0) scheme(s1mono) rename (b2005 = 2005 b2006 = 2006 ///
b2007 = 2007 b2008 = 2008 b2009 = 2009 b2010 = 2010 b2011 = 2011 b2012 = 2012 ///
b2013 = 2013 b2014 = 2014 b2015 = 2015 b2016 = 2016) xline(6.5, lpattern(dash)) ///
title("E", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical))
graph save "drc_model2_deadly.gph", replace

gr combine drc_model2_all_conflict.gph drc_model2_civilians.gph drc_model2_battles.gph ///
drc_model2_riots_protest.gph drc_model2_deadly.gph, scheme(s1mono) cols(2) ycommon

*** Permutation tests for specification 2, only DRC

* All conflict
use "conflict_panel.dta", clear
drop group
gen group=1 if country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | ///
country=="South Africa"| country=="Nigeria" | country=="Senegal" | ///
country=="Guinea" | country=="Sierra Leone" | country=="Liberia" | ///
country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | ///
country=="Burkina Faso" | country=="Eritrea" | country=="Djibouti" | ///
country=="Lesotho" | country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.
drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace
save "conflict_panel_placebo_2.dta", replace
collapse (mean) group, by(country)
drop group
gen effect_5 = .
gen effect_6 = .
gen effect_7 = .
gen effect_8 = .
gen effect_9 = .
gen effect_10 = .
gen effect_11 = .
gen effect_12 = .
gen effect_13 = .
gen effect_14 = .
gen effect_15 = .
gen effect_16 = .
egen country_num = group(country)
su country_num, meanonly
foreach i of num 1/`r(max)' {
	preserve
	
	use "conflict_panel_placebo_2.dta", clear
	egen country_num = group(country)
	
	drop group
	gen group=1 if country_num==`i'
	replace group=0 if country_num!=`i'
	drop if group==.

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen post2010=1 if year_month>201007
	replace post2010=0 if post2010==.
	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_flag==.
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0
	gen did = group*post2010
	encode country, generate(country_id)
	egen country_admin1 = concat(country admin1)
	encode country_admin1, generate(admin1_id)
	egen country_admin1_2 = concat(country admin1 admin2)
	encode country_admin1_2, generate(admin2_id)
	set matsize 10000
	
	gen b2004 = 1 if year==2004 & group==1
	replace b2004 = 0 if b2004==.
	gen b2005 = 1 if year==2005 & group==1
	replace b2005 = 0 if b2005==.
	gen b2006 = 1 if year==2006 & group==1
	replace b2006 = 0 if b2006==.
	gen b2007 = 1 if year==2007 & group==1
	replace b2007 = 0 if b2007==.
	gen b2008 = 1 if year==2008 & group==1
	replace b2008 = 0 if b2008==.
	gen b2009 = 1 if year==2009 & group==1
	replace b2009 = 0 if b2009==.
	gen b2010 = 1 if year==2010 & group==1
	replace b2010 = 0 if b2010==.
	gen b2011 = 1 if year==2011 & group==1
	replace b2011 = 0 if b2011==.
	gen b2012 = 1 if year==2012 & group==1
	replace b2012 = 0 if b2012==.
	gen b2013 = 1 if year==2013 & group==1
	replace b2013 = 0 if b2013==.
	gen b2014 = 1 if year==2014 & group==1
	replace b2014 = 0 if b2014==.
	gen b2015 = 1 if year==2015 & group==1
	replace b2015 = 0 if b2015==.
	gen b2016 = 1 if year==2016 & group==1
	replace b2016 = 0 if b2016==.

	reg conflict_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
	b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)
	
	local effect_5 = _b[b2005]
	local effect_6 = _b[b2006]
	local effect_7 = _b[b2007]
	local effect_8 = _b[b2008]
	local effect_9 = _b[b2009]
	local effect_10 = _b[b2010]
	local effect_11 = _b[b2011]
	local effect_12 = _b[b2012]
	local effect_13 = _b[b2013]
	local effect_14 = _b[b2014]
	local effect_15 = _b[b2015]
	local effect_16 = _b[b2016]
	
	restore
	
	replace effect_5 = `effect_5' in `i'
	replace effect_6 = `effect_6' in `i'
	replace effect_7 = `effect_7' in `i'
	replace effect_8 = `effect_8' in `i'
	replace effect_9 = `effect_9' in `i'
	replace effect_10 = `effect_10' in `i'
	replace effect_11 = `effect_11' in `i'
	replace effect_12 = `effect_12' in `i'
	replace effect_13 = `effect_13' in `i'
	replace effect_14 = `effect_14' in `i'
	replace effect_15 = `effect_15' in `i'
	replace effect_16 = `effect_16' in `i'
}
*
save "placebo_all_conflict_2.dta", replace
use "placebo_all_conflict_2.dta", clear
forvalues i = 5(1)16 {
	sum effect_`i' if country!="Democratic Republic of Congo", detail 
	local p5_`i' = r(p5)
	local p95_`i' = r(p95)
}
*
drop country_num
reshape long effect_, i(country) j(t)
keep if country=="Democratic Republic of Congo"
gen p5 = .
gen p95 = .
forvalues i = 5(1)16 {
	replace p5 = `p5_`i'' if t==`i'
	replace p95 = `p95_`i'' if t==`i'
}
*
twoway (sc effect_ t, yline(0) scheme(s1mono) xlabel(5 "2005" 6 "2006" ///
7 "2007" 8 "2008" 9 "2009" 10 "2010" 11 "2011" 12 "2012" ///
13 "2013" 14 "2014" 15 "2015" 16 "2016") xline(10.5, lpattern(dash)) ///
title("A", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical)) legend(order(1 ///
"Effect Estimate" 2 "5th-95th Percentile on Placebo Distribution"))) ///
	(rcap p5 p95 t)
graph save "drc_model2_all_conflict.gph", replace

* Violence Against Civilians
use "conflict_panel.dta", clear
drop group
gen group=1 if country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | ///
country=="South Africa"| country=="Nigeria" | country=="Senegal" | ///
country=="Guinea" | country=="Sierra Leone" | country=="Liberia" | ///
country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | ///
country=="Burkina Faso" | country=="Eritrea" | country=="Djibouti" | ///
country=="Lesotho" | country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.
drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace
save "conflict_panel_placebo_2.dta", replace
collapse (mean) group, by(country)
drop group
gen effect_5 = .
gen effect_6 = .
gen effect_7 = .
gen effect_8 = .
gen effect_9 = .
gen effect_10 = .
gen effect_11 = .
gen effect_12 = .
gen effect_13 = .
gen effect_14 = .
gen effect_15 = .
gen effect_16 = .
egen country_num = group(country)
su country_num, meanonly
foreach i of num 1/`r(max)' {
	preserve
	
	use "conflict_panel_placebo_2.dta", clear
	egen country_num = group(country)
	
	drop group
	gen group=1 if country_num==`i'
	replace group=0 if country_num!=`i'
	drop if group==.

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen post2010=1 if year_month>201007
	replace post2010=0 if post2010==.
	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_flag==.
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0
	gen did = group*post2010
	encode country, generate(country_id)
	egen country_admin1 = concat(country admin1)
	encode country_admin1, generate(admin1_id)
	egen country_admin1_2 = concat(country admin1 admin2)
	encode country_admin1_2, generate(admin2_id)
	set matsize 10000
	
	gen b2004 = 1 if year==2004 & group==1
	replace b2004 = 0 if b2004==.
	gen b2005 = 1 if year==2005 & group==1
	replace b2005 = 0 if b2005==.
	gen b2006 = 1 if year==2006 & group==1
	replace b2006 = 0 if b2006==.
	gen b2007 = 1 if year==2007 & group==1
	replace b2007 = 0 if b2007==.
	gen b2008 = 1 if year==2008 & group==1
	replace b2008 = 0 if b2008==.
	gen b2009 = 1 if year==2009 & group==1
	replace b2009 = 0 if b2009==.
	gen b2010 = 1 if year==2010 & group==1
	replace b2010 = 0 if b2010==.
	gen b2011 = 1 if year==2011 & group==1
	replace b2011 = 0 if b2011==.
	gen b2012 = 1 if year==2012 & group==1
	replace b2012 = 0 if b2012==.
	gen b2013 = 1 if year==2013 & group==1
	replace b2013 = 0 if b2013==.
	gen b2014 = 1 if year==2014 & group==1
	replace b2014 = 0 if b2014==.
	gen b2015 = 1 if year==2015 & group==1
	replace b2015 = 0 if b2015==.
	gen b2016 = 1 if year==2016 & group==1
	replace b2016 = 0 if b2016==.

	reg civilians_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
	b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)
	
	local effect_5 = _b[b2005]
	local effect_6 = _b[b2006]
	local effect_7 = _b[b2007]
	local effect_8 = _b[b2008]
	local effect_9 = _b[b2009]
	local effect_10 = _b[b2010]
	local effect_11 = _b[b2011]
	local effect_12 = _b[b2012]
	local effect_13 = _b[b2013]
	local effect_14 = _b[b2014]
	local effect_15 = _b[b2015]
	local effect_16 = _b[b2016]
	
	restore
	
	replace effect_5 = `effect_5' in `i'
	replace effect_6 = `effect_6' in `i'
	replace effect_7 = `effect_7' in `i'
	replace effect_8 = `effect_8' in `i'
	replace effect_9 = `effect_9' in `i'
	replace effect_10 = `effect_10' in `i'
	replace effect_11 = `effect_11' in `i'
	replace effect_12 = `effect_12' in `i'
	replace effect_13 = `effect_13' in `i'
	replace effect_14 = `effect_14' in `i'
	replace effect_15 = `effect_15' in `i'
	replace effect_16 = `effect_16' in `i'
}
*
save "placebo_civilians_2.dta", replace
use "placebo_civilians_2.dta", clear
forvalues i = 5(1)16 {
	sum effect_`i' if country!="Democratic Republic of Congo", detail 
	local p5_`i' = r(p5)
	local p95_`i' = r(p95)
}
*
drop country_num
reshape long effect_, i(country) j(t)
keep if country=="Democratic Republic of Congo"
gen p5 = .
gen p95 = .
forvalues i = 5(1)16 {
	replace p5 = `p5_`i'' if t==`i'
	replace p95 = `p95_`i'' if t==`i'
}
*
twoway (sc effect_ t, yline(0) scheme(s1mono) xlabel(5 "2005" 6 "2006" ///
7 "2007" 8 "2008" 9 "2009" 10 "2010" 11 "2011" 12 "2012" ///
13 "2013" 14 "2014" 15 "2015" 16 "2016") xline(10.5, lpattern(dash)) ///
title("B", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical)) legend(order(1 ///
"Effect Estimate" 2 "5th-95th Percentile on Placebo Distribution"))) ///
	(rcap p5 p95 t)
graph save "drc_model2_civilians.gph", replace

* Battles
use "conflict_panel.dta", clear
drop group
gen group=1 if country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | ///
country=="South Africa"| country=="Nigeria" | country=="Senegal" | ///
country=="Guinea" | country=="Sierra Leone" | country=="Liberia" | ///
country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | ///
country=="Burkina Faso" | country=="Eritrea" | country=="Djibouti" | ///
country=="Lesotho" | country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.
drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace
save "conflict_panel_placebo_2.dta", replace
collapse (mean) group, by(country)
drop group
gen effect_5 = .
gen effect_6 = .
gen effect_7 = .
gen effect_8 = .
gen effect_9 = .
gen effect_10 = .
gen effect_11 = .
gen effect_12 = .
gen effect_13 = .
gen effect_14 = .
gen effect_15 = .
gen effect_16 = .
egen country_num = group(country)
su country_num, meanonly
foreach i of num 1/`r(max)' {
	preserve
	
	use "conflict_panel_placebo_2.dta", clear
	egen country_num = group(country)
	
	drop group
	gen group=1 if country_num==`i'
	replace group=0 if country_num!=`i'
	drop if group==.

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen post2010=1 if year_month>201007
	replace post2010=0 if post2010==.
	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_flag==.
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0
	gen did = group*post2010
	encode country, generate(country_id)
	egen country_admin1 = concat(country admin1)
	encode country_admin1, generate(admin1_id)
	egen country_admin1_2 = concat(country admin1 admin2)
	encode country_admin1_2, generate(admin2_id)
	set matsize 10000
	
	gen b2004 = 1 if year==2004 & group==1
	replace b2004 = 0 if b2004==.
	gen b2005 = 1 if year==2005 & group==1
	replace b2005 = 0 if b2005==.
	gen b2006 = 1 if year==2006 & group==1
	replace b2006 = 0 if b2006==.
	gen b2007 = 1 if year==2007 & group==1
	replace b2007 = 0 if b2007==.
	gen b2008 = 1 if year==2008 & group==1
	replace b2008 = 0 if b2008==.
	gen b2009 = 1 if year==2009 & group==1
	replace b2009 = 0 if b2009==.
	gen b2010 = 1 if year==2010 & group==1
	replace b2010 = 0 if b2010==.
	gen b2011 = 1 if year==2011 & group==1
	replace b2011 = 0 if b2011==.
	gen b2012 = 1 if year==2012 & group==1
	replace b2012 = 0 if b2012==.
	gen b2013 = 1 if year==2013 & group==1
	replace b2013 = 0 if b2013==.
	gen b2014 = 1 if year==2014 & group==1
	replace b2014 = 0 if b2014==.
	gen b2015 = 1 if year==2015 & group==1
	replace b2015 = 0 if b2015==.
	gen b2016 = 1 if year==2016 & group==1
	replace b2016 = 0 if b2016==.

	reg battles_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
	b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)
	
	local effect_5 = _b[b2005]
	local effect_6 = _b[b2006]
	local effect_7 = _b[b2007]
	local effect_8 = _b[b2008]
	local effect_9 = _b[b2009]
	local effect_10 = _b[b2010]
	local effect_11 = _b[b2011]
	local effect_12 = _b[b2012]
	local effect_13 = _b[b2013]
	local effect_14 = _b[b2014]
	local effect_15 = _b[b2015]
	local effect_16 = _b[b2016]
	
	restore
	
	replace effect_5 = `effect_5' in `i'
	replace effect_6 = `effect_6' in `i'
	replace effect_7 = `effect_7' in `i'
	replace effect_8 = `effect_8' in `i'
	replace effect_9 = `effect_9' in `i'
	replace effect_10 = `effect_10' in `i'
	replace effect_11 = `effect_11' in `i'
	replace effect_12 = `effect_12' in `i'
	replace effect_13 = `effect_13' in `i'
	replace effect_14 = `effect_14' in `i'
	replace effect_15 = `effect_15' in `i'
	replace effect_16 = `effect_16' in `i'
}
*
save "placebo_battles_2.dta", replace
use "placebo_battles_2.dta", clear
forvalues i = 5(1)16 {
	sum effect_`i' if country!="Democratic Republic of Congo", detail 
	local p5_`i' = r(p5)
	local p95_`i' = r(p95)
}
*
drop country_num
reshape long effect_, i(country) j(t)
keep if country=="Democratic Republic of Congo"
gen p5 = .
gen p95 = .
forvalues i = 5(1)16 {
	replace p5 = `p5_`i'' if t==`i'
	replace p95 = `p95_`i'' if t==`i'
}
*
twoway (sc effect_ t, yline(0) scheme(s1mono) xlabel(5 "2005" 6 "2006" ///
7 "2007" 8 "2008" 9 "2009" 10 "2010" 11 "2011" 12 "2012" ///
13 "2013" 14 "2014" 15 "2015" 16 "2016") xline(10.5, lpattern(dash)) ///
title("C", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical)) legend(order(1 ///
"Effect Estimate" 2 "5th-95th Percentile on Placebo Distribution"))) ///
	(rcap p5 p95 t)
graph save "drc_model2_battles.gph", replace

* Riots and Protests
use "conflict_panel.dta", clear
drop group
gen group=1 if country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | ///
country=="South Africa"| country=="Nigeria" | country=="Senegal" | ///
country=="Guinea" | country=="Sierra Leone" | country=="Liberia" | ///
country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | ///
country=="Burkina Faso" | country=="Eritrea" | country=="Djibouti" | ///
country=="Lesotho" | country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.
drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace
save "conflict_panel_placebo_2.dta", replace
collapse (mean) group, by(country)
drop group
gen effect_5 = .
gen effect_6 = .
gen effect_7 = .
gen effect_8 = .
gen effect_9 = .
gen effect_10 = .
gen effect_11 = .
gen effect_12 = .
gen effect_13 = .
gen effect_14 = .
gen effect_15 = .
gen effect_16 = .
egen country_num = group(country)
su country_num, meanonly
foreach i of num 1/`r(max)' {
	preserve
	
	use "conflict_panel_placebo_2.dta", clear
	egen country_num = group(country)
	
	drop group
	gen group=1 if country_num==`i'
	replace group=0 if country_num!=`i'
	drop if group==.

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen post2010=1 if year_month>201007
	replace post2010=0 if post2010==.
	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_flag==.
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen riots_protest_flag=1 if riots_protest>0
	replace riots_protest_flag=0 if riots_protest==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0
	gen did = group*post2010
	encode country, generate(country_id)
	egen country_admin1 = concat(country admin1)
	encode country_admin1, generate(admin1_id)
	egen country_admin1_2 = concat(country admin1 admin2)
	encode country_admin1_2, generate(admin2_id)
	set matsize 10000
	
	gen b2004 = 1 if year==2004 & group==1
	replace b2004 = 0 if b2004==.
	gen b2005 = 1 if year==2005 & group==1
	replace b2005 = 0 if b2005==.
	gen b2006 = 1 if year==2006 & group==1
	replace b2006 = 0 if b2006==.
	gen b2007 = 1 if year==2007 & group==1
	replace b2007 = 0 if b2007==.
	gen b2008 = 1 if year==2008 & group==1
	replace b2008 = 0 if b2008==.
	gen b2009 = 1 if year==2009 & group==1
	replace b2009 = 0 if b2009==.
	gen b2010 = 1 if year==2010 & group==1
	replace b2010 = 0 if b2010==.
	gen b2011 = 1 if year==2011 & group==1
	replace b2011 = 0 if b2011==.
	gen b2012 = 1 if year==2012 & group==1
	replace b2012 = 0 if b2012==.
	gen b2013 = 1 if year==2013 & group==1
	replace b2013 = 0 if b2013==.
	gen b2014 = 1 if year==2014 & group==1
	replace b2014 = 0 if b2014==.
	gen b2015 = 1 if year==2015 & group==1
	replace b2015 = 0 if b2015==.
	gen b2016 = 1 if year==2016 & group==1
	replace b2016 = 0 if b2016==.

	reg riots_protest_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
	b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)
	
	local effect_5 = _b[b2005]
	local effect_6 = _b[b2006]
	local effect_7 = _b[b2007]
	local effect_8 = _b[b2008]
	local effect_9 = _b[b2009]
	local effect_10 = _b[b2010]
	local effect_11 = _b[b2011]
	local effect_12 = _b[b2012]
	local effect_13 = _b[b2013]
	local effect_14 = _b[b2014]
	local effect_15 = _b[b2015]
	local effect_16 = _b[b2016]
	
	restore
	
	replace effect_5 = `effect_5' in `i'
	replace effect_6 = `effect_6' in `i'
	replace effect_7 = `effect_7' in `i'
	replace effect_8 = `effect_8' in `i'
	replace effect_9 = `effect_9' in `i'
	replace effect_10 = `effect_10' in `i'
	replace effect_11 = `effect_11' in `i'
	replace effect_12 = `effect_12' in `i'
	replace effect_13 = `effect_13' in `i'
	replace effect_14 = `effect_14' in `i'
	replace effect_15 = `effect_15' in `i'
	replace effect_16 = `effect_16' in `i'
}
*
save "placebo_riots_protest_2.dta", replace
use "placebo_riots_protest_2.dta", clear
forvalues i = 5(1)16 {
	sum effect_`i' if country!="Democratic Republic of Congo", detail 
	local p5_`i' = r(p5)
	local p95_`i' = r(p95)
}
*
drop country_num
reshape long effect_, i(country) j(t)
keep if country=="Democratic Republic of Congo"
gen p5 = .
gen p95 = .
forvalues i = 5(1)16 {
	replace p5 = `p5_`i'' if t==`i'
	replace p95 = `p95_`i'' if t==`i'
}
*
twoway (sc effect_ t, yline(0) scheme(s1mono) xlabel(5 "2005" 6 "2006" ///
7 "2007" 8 "2008" 9 "2009" 10 "2010" 11 "2011" 12 "2012" ///
13 "2013" 14 "2014" 15 "2015" 16 "2016") xline(10.5, lpattern(dash)) ///
title("D", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical)) legend(order(1 ///
"Effect Estimate" 2 "5th-95th Percentile on Placebo Distribution"))) ///
	(rcap p5 p95 t)
graph save "drc_model2_riots_protest.gph", replace

* Deadly Conflict
use "conflict_panel.dta", clear
drop group
gen group=1 if country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | ///
country=="South Africa"| country=="Nigeria" | country=="Senegal" | ///
country=="Guinea" | country=="Sierra Leone" | country=="Liberia" | ///
country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | ///
country=="Burkina Faso" | country=="Eritrea" | country=="Djibouti" | ///
country=="Lesotho" | country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.
drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace
save "conflict_panel_placebo_2.dta", replace
collapse (mean) group, by(country)
drop group
gen effect_5 = .
gen effect_6 = .
gen effect_7 = .
gen effect_8 = .
gen effect_9 = .
gen effect_10 = .
gen effect_11 = .
gen effect_12 = .
gen effect_13 = .
gen effect_14 = .
gen effect_15 = .
gen effect_16 = .
egen country_num = group(country)
su country_num, meanonly
foreach i of num 1/`r(max)' {
	preserve
	
	use "conflict_panel_placebo_2.dta", clear
	egen country_num = group(country)
	
	drop group
	gen group=1 if country_num==`i'
	replace group=0 if country_num!=`i'
	drop if group==.

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen post2010=1 if year_month>201007
	replace post2010=0 if post2010==.
	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_flag==.
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0
	gen did = group*post2010
	encode country, generate(country_id)
	egen country_admin1 = concat(country admin1)
	encode country_admin1, generate(admin1_id)
	egen country_admin1_2 = concat(country admin1 admin2)
	encode country_admin1_2, generate(admin2_id)
	set matsize 10000
	
	gen b2004 = 1 if year==2004 & group==1
	replace b2004 = 0 if b2004==.
	gen b2005 = 1 if year==2005 & group==1
	replace b2005 = 0 if b2005==.
	gen b2006 = 1 if year==2006 & group==1
	replace b2006 = 0 if b2006==.
	gen b2007 = 1 if year==2007 & group==1
	replace b2007 = 0 if b2007==.
	gen b2008 = 1 if year==2008 & group==1
	replace b2008 = 0 if b2008==.
	gen b2009 = 1 if year==2009 & group==1
	replace b2009 = 0 if b2009==.
	gen b2010 = 1 if year==2010 & group==1
	replace b2010 = 0 if b2010==.
	gen b2011 = 1 if year==2011 & group==1
	replace b2011 = 0 if b2011==.
	gen b2012 = 1 if year==2012 & group==1
	replace b2012 = 0 if b2012==.
	gen b2013 = 1 if year==2013 & group==1
	replace b2013 = 0 if b2013==.
	gen b2014 = 1 if year==2014 & group==1
	replace b2014 = 0 if b2014==.
	gen b2015 = 1 if year==2015 & group==1
	replace b2015 = 0 if b2015==.
	gen b2016 = 1 if year==2016 & group==1
	replace b2016 = 0 if b2016==.

	reg deadly_conflict_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
	b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)
	
	local effect_5 = _b[b2005]
	local effect_6 = _b[b2006]
	local effect_7 = _b[b2007]
	local effect_8 = _b[b2008]
	local effect_9 = _b[b2009]
	local effect_10 = _b[b2010]
	local effect_11 = _b[b2011]
	local effect_12 = _b[b2012]
	local effect_13 = _b[b2013]
	local effect_14 = _b[b2014]
	local effect_15 = _b[b2015]
	local effect_16 = _b[b2016]
	
	restore
	
	replace effect_5 = `effect_5' in `i'
	replace effect_6 = `effect_6' in `i'
	replace effect_7 = `effect_7' in `i'
	replace effect_8 = `effect_8' in `i'
	replace effect_9 = `effect_9' in `i'
	replace effect_10 = `effect_10' in `i'
	replace effect_11 = `effect_11' in `i'
	replace effect_12 = `effect_12' in `i'
	replace effect_13 = `effect_13' in `i'
	replace effect_14 = `effect_14' in `i'
	replace effect_15 = `effect_15' in `i'
	replace effect_16 = `effect_16' in `i'
}
*
save "placebo_deadly_conflict_2.dta", replace
use "placebo_deadly_conflict_2.dta", clear
forvalues i = 5(1)16 {
	sum effect_`i' if country!="Democratic Republic of Congo", detail 
	local p5_`i' = r(p5)
	local p95_`i' = r(p95)
}
*
drop country_num
reshape long effect_, i(country) j(t)
keep if country=="Democratic Republic of Congo"
gen p5 = .
gen p95 = .
forvalues i = 5(1)16 {
	replace p5 = `p5_`i'' if t==`i'
	replace p95 = `p95_`i'' if t==`i'
}
*
twoway (sc effect_ t, yline(0) scheme(s1mono) xlabel(5 "2005" 6 "2006" ///
7 "2007" 8 "2008" 9 "2009" 10 "2010" 11 "2011" 12 "2012" ///
13 "2013" 14 "2014" 15 "2015" 16 "2016") xline(10.5, lpattern(dash)) ///
title("E", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical)) legend(order(1 ///
"Effect Estimate" 2 "5th-95th Percentile on Placebo Distribution"))) ///
	(rcap p5 p95 t)
graph save "drc_model2_deadly.gph", replace

grc1leg drc_model2_all_conflict.gph drc_model2_civilians.gph drc_model2_battles.gph ///
drc_model2_riots_protest.gph drc_model2_deadly.gph, scheme(s1mono) cols(2) ycommon

grc1leg trend_all_conflict.gph trend_civilians.gph trend_battles.gph ///
trend_riots_protest.gph trend_deadly.gph, scheme(s1mono) cols(2) ycommon

* All Dodd Frank
use "conflict_panel.dta", clear

drop group

gen group=1 if country=="Uganda" | country=="Rwanda" | country=="Burundi" | ///
country=="Tanzania" |  | country=="Zambia" | ///
country=="Angola" | country=="Republic of Congo" | ///
country=="Central African Republic" | country=="Democratic Republic of Congo"
replace group=0 if country=="Kenya" | country=="Ethiopia" | country=="Chad" | ///
country=="Cameroon" | country=="Gabon" | country=="Mozambique" | country=="Malawi" | ///
country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | country=="South Africa" | ///
country=="Nigeria" | country=="Senegal" | country=="Guinea" | country=="Sierra Leone" | ///
country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | country=="Gambia" | ///
country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | country=="Burkina Faso" | ///
country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"
drop if group==.

drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace

gen post2010=1 if year_month>201007
replace post2010=0 if post2010==.
gen conflict_flag=1 if conflict_count>0
replace conflict_flag=0 if conflict_flag==.
gen civilians_flag=1 if civilians>0
replace civilians_flag=0 if civilians==0
gen battles_flag=1 if battles>0
replace battles_flag=0 if battles==0
gen riots_protest_flag=1 if riots_protest>0
replace riots_protest_flag=0 if riots_protest==0
gen other_flag=1 if other>0
replace other_flag=0 if other==0
gen deadly_conflict_flag=1 if fatalities>0
replace deadly_conflict_flag=0 if fatalities==0
gen did = group*post2010
encode country, generate(country_id)
egen country_admin1 = concat(country admin1)
encode country_admin1, generate(admin1_id)
egen country_admin1_2 = concat(country admin1 admin2)
encode country_admin1_2, generate(admin2_id)
set matsize 10000


* Specification (2) 
gen b2004 = 1 if year==2004 & group==1
replace b2004 = 0 if b2004==.
gen b2005 = 1 if year==2005 & group==1
replace b2005 = 0 if b2005==.
gen b2006 = 1 if year==2006 & group==1
replace b2006 = 0 if b2006==.
gen b2007 = 1 if year==2007 & group==1
replace b2007 = 0 if b2007==.
gen b2008 = 1 if year==2008 & group==1
replace b2008 = 0 if b2008==.
gen b2009 = 1 if year==2009 & group==1
replace b2009 = 0 if b2009==.
gen b2010 = 1 if year==2010 & group==1
replace b2010 = 0 if b2010==.
gen b2011 = 1 if year==2011 & group==1
replace b2011 = 0 if b2011==.
gen b2012 = 1 if year==2012 & group==1
replace b2012 = 0 if b2012==.
gen b2013 = 1 if year==2013 & group==1
replace b2013 = 0 if b2013==.
gen b2014 = 1 if year==2014 & group==1
replace b2014 = 0 if b2014==.
gen b2015 = 1 if year==2015 & group==1
replace b2015 = 0 if b2015==.
gen b2016 = 1 if year==2016 & group==1
replace b2016 = 0 if b2016==.

reg conflict_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)

coefplot, vertical keep(b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016) yline(0) scheme(s1mono) rename (b2005 = 2005 b2006 = 2006 ///
b2007 = 2007 b2008 = 2008 b2009 = 2009 b2010 = 2010 b2011 = 2011 b2012 = 2012 ///
b2013 = 2013 b2014 = 2014 b2015 = 2015 b2016 = 2016) xline(6.5, lpattern(dash)) ///
title("A", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical))
graph save "alldf_model2_all_conflict.gph", replace

reg civilians_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)

coefplot, vertical keep(b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016) yline(0) scheme(s1mono) rename (b2005 = 2005 b2006 = 2006 ///
b2007 = 2007 b2008 = 2008 b2009 = 2009 b2010 = 2010 b2011 = 2011 b2012 = 2012 ///
b2013 = 2013 b2014 = 2014 b2015 = 2015 b2016 = 2016) xline(6.5, lpattern(dash)) ///
title("B", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical))
graph save "alldf_model2_civilians.gph", replace

reg battles_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)

coefplot, vertical keep(b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016) yline(0) scheme(s1mono) rename (b2005 = 2005 b2006 = 2006 ///
b2007 = 2007 b2008 = 2008 b2009 = 2009 b2010 = 2010 b2011 = 2011 b2012 = 2012 ///
b2013 = 2013 b2014 = 2014 b2015 = 2015 b2016 = 2016) xline(6.5, lpattern(dash)) ///
title("C", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical))
graph save "alldf_model2_battles.gph", replace

reg riots_protest_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)

coefplot, vertical keep(b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016) yline(0) scheme(s1mono) rename (b2005 = 2005 b2006 = 2006 ///
b2007 = 2007 b2008 = 2008 b2009 = 2009 b2010 = 2010 b2011 = 2011 b2012 = 2012 ///
b2013 = 2013 b2014 = 2014 b2015 = 2015 b2016 = 2016) xline(6.5, lpattern(dash)) ///
title("D", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical))
graph save "alldf_model2_riots_protests.gph", replace

reg deadly_conflict_flag b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016 i.year_month i.admin1_id, vce(cluster country_id)

coefplot, vertical keep(b2005 b2006 b2007 b2008 b2009 b2010 b2011 b2012 b2013 ///
b2014 b2015 b2016) yline(0) scheme(s1mono) rename (b2005 = 2005 b2006 = 2006 ///
b2007 = 2007 b2008 = 2008 b2009 = 2009 b2010 = 2010 b2011 = 2011 b2012 = 2012 ///
b2013 = 2013 b2014 = 2014 b2015 = 2015 b2016 = 2016) xline(6.5, lpattern(dash)) ///
title("E", position(10)) xtitle("Year") ytitle("Effect Size") ///
ylabel(, angle(horizontal)) xlabel(, angle(vertical))
graph save "alldf_model2_deadly.gph", replace

gr combine alldf_model2_all_conflict.gph alldf_model2_civilians.gph ///
alldf_model2_battles.gph alldf_model2_riots_protests.gph alldf_model2_deadly.gph, ///
scheme(s1mono) cols(2) ycommon

* Robustness Check (Synthetic Control)
use "conflict_panel.dta", clear

drop year_month
gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
destring year_month, replace

gen conflict_flag=1 if conflict_count>0
replace conflict_flag=0 if conflict_count==0
gen civilians_flag=1 if civilians>0
replace civilians_flag=0 if civilians==0
gen battles_flag=1 if battles>0
replace battles_flag=0 if battles==0
gen riots_protest_flag=1 if riots_protest>0
replace riots_protest_flag=0 if riots_protest==0
gen other_flag=1 if other>0
replace other_flag=0 if other==0
gen deadly_conflict_flag=1 if fatalities>0
replace deadly_conflict_flag=0 if fatalities==0

egen country_admin1 = concat(country admin1)

* Define donor pool
drop group

keep if country=="Democratic Republic of Congo" | country=="Kenya" | country=="Ethiopia" | ///
country=="Chad" | country=="Cameroon" | country=="Gabon" | country=="Mozambique" | ///
country=="Malawi" | country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | ///
country=="South Africa" | country=="Nigeria" | country=="Senegal" | country=="Guinea" | ///
country=="Sierra Leone" | country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | ///
country=="Gambia" | country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | ///
country=="Burkina Faso" | country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
country=="Swaziland" | country=="Niger" | country=="Mali"

replace country="DRC" if country=="Democratic Republic of Congo"


encode country_admin1, generate(geo1_id)
label drop geo1_id
tostring year_month, generate(year_month_str)
drop year_month
encode year_month_str, generate (year_month)
encode country, generate(country_id)
replace geo1_id = 75 if country=="DRC"

collapse (mean) conflict_flag civilians_flag battles_flag riots_protest_flag ///
deadly_conflict_flag, by(geo1_id year_month)

tsset geo1_id year_month

* Generate moving average
gen ma_conflict = (F5.conflict_flag + F4.conflict_flag + F3.conflict_flag + ///
F2.conflict_flag + F1.conflict_flag + conflict_flag + L1.conflict_flag + ///
L2.conflict_flag + L3.conflict_flag + L4.conflict_flag + L5.conflict_flag)/11

gen ma_civilians = (F5.civilians_flag + F4.civilians_flag + F3.civilians_flag + ///
F2.civilians_flag + F1.civilians_flag + civilians_flag + L1.civilians_flag + ///
L2.civilians_flag + L3.civilians_flag + L4.civilians_flag + L5.civilians_flag)/11

gen ma_battles = (F5.battles_flag + F4.battles_flag + F3.battles_flag + ///
F2.battles_flag + F1.battles_flag + battles_flag + L1.battles_flag + ///
L2.battles_flag + L3.battles_flag + L4.battles_flag + L5.battles_flag)/11

gen ma_riots_protests = (F5.riots_protest_flag + F4.riots_protest_flag + F3.riots_protest_flag + ///
F2.riots_protest_flag + F1.riots_protest_flag + riots_protest_flag + L1.riots_protest_flag + ///
L2.riots_protest_flag + L3.riots_protest_flag + L4.riots_protest_flag + L5.riots_protest_flag)/11

gen ma_deadly = (F5.deadly_conflict_flag + F4.deadly_conflict_flag + F3.deadly_conflict_flag + ///
F2.deadly_conflict_flag + F1.deadly_conflict_flag + deadly_conflict_flag + L1.deadly_conflict_flag + ///
L2.deadly_conflict_flag + L3.deadly_conflict_flag + L4.deadly_conflict_flag + L5.deadly_conflict_flag)/11

drop if ma_conflict==.

tsset geo1_id year_month

synth ma_conflict ma_conflict, trunit(75) trperiod(79) keep(synth_allconflict.dta) replace fig
mat list e(V_matrix)
graph save Graph synth_allconflict.gph, replace

synth ma_civilians ma_civilians, trunit(75) trperiod(79) keep(synth_civilians.dta) replace fig
mat list e(V_matrix)
graph save Graph synth_civilians.gph, replace

synth ma_battles ma_battles, trunit(75) trperiod(79) keep(synth_battles.dta) replace fig
mat list e(V_matrix)
graph save Graph synth_battles.gph, replace

synth ma_riots_protests ma_riots_protests, trunit(75) trperiod(79) keep(synth_riots_protest.dta) replace fig
mat list e(V_matrix)
graph save Graph synth_riots_protest.gph, replace

synth ma_deadly ma_deadly, trunit(75) trperiod(79) keep(synth_deadly_conflict.dta) replace fig
mat list e(V_matrix)
graph save Graph synth_deadly_conflict.gph, replace

*** All Conflict ***
* Plot the gap in predicted error
use synth_allconflict.dta, clear
keep _Y_treated _Y_synthetic _time
drop if _time==.
rename _time month
rename _Y_treated  treat
rename _Y_synthetic counterfact
gen gap=treat-counterfact
sort month 

* Synthetic control effect estimates
egen postmean1 = mean(gap) if month>=79
egen premean1 = mean(gap) if month<79
egen postgap = mean(postmean1)
egen pregap = mean(premean1)
gen effect = postgap - pregap
sum effect

* Calculate RMSPE ratio
gen gap3=gap*gap
egen postmean=mean(gap3) if month>79
egen premean=mean(gap3) if month<=79
gen rmspe=sqrt(premean) if month<=79
replace rmspe=sqrt(postmean) if month>79
gen ratio=rmspe/rmspe[_n-1] if month==80
gen rmspe_post=sqrt(postmean) if month>79
gen rmspe_pre=rmspe[_n-1] if month==79

* Plot the treated vs. synthetic control
line treat month, lp(solid)lcolor(black) || line counterfact month, ///
lp(dash)lcolor(black) scheme(s1mono) xline(79) ylabel(0(0.25)0.5, angle(horizontal)) ///
title("A", position(10)) xtitle("Year") ytitle("Probability of Conflict") ///
legend(label(1 "DRC")) legend(label(2 "Synthetic DRC")) xlabel(0 "2004" 13 "2005" ///
25 "2006" 37 "2007" 49 "2008" 61 "2009" 73 "2010" 85 "2011" 97 "2012" 109 "2013" ///
121 "2014" 133 "2015" 145 "2016", angle(vertical))
graph save synth_allconflict_A.gph, replace

* Plot the gap in predicted error
line gap month, lp(solid)lcolor(black) yline(0, lp(solid)lw(vthin)lcolor(black)) ///
xline(79, lp(solid)lw(vthin)lcolor(black)) xtitle("",si(medsmall)) xlabel(#10) ///
ytitle("Gap in Probability of Conflict", size(medsmall)) ///
xlabel(0 "2004" 13 "2005" 25 "2006" 37 "2007" 49 "2008" 61 "2009" 73 "2010" 85 ///
"2011" 97 "2012" 109 "2013" 121 "2014" 133 "2015" 145 "2016", angle(vertical)) ///
scheme(s1mono) title("B", position(10)) legend(off)
graph save synth_allconflict_B.gph, replace

*** Violence against civilians
* Plot the gap in predicted error
use synth_civilians.dta, clear
keep _Y_treated _Y_synthetic _time
drop if _time==.
rename _time month 
rename _Y_treated  treat
rename _Y_synthetic counterfact
gen gap=treat-counterfact
sort month 

* Synthetic control effect estimates
egen postmean1 = mean(gap) if month>=79
egen premean1 = mean(gap) if month<79
egen postgap = mean(postmean1)
egen pregap = mean(premean1)
gen effect = postgap - pregap
sum effect

* Calculate RMSPE ratio
gen gap3=gap*gap
egen postmean=mean(gap3) if month>79
egen premean=mean(gap3) if month<=79
gen rmspe=sqrt(premean) if month<=79
replace rmspe=sqrt(postmean) if month>79
gen ratio=rmspe/rmspe[_n-1] if month==80
gen rmspe_post=sqrt(postmean) if month>79
gen rmspe_pre=rmspe[_n-1] if month==79

* Plot the treated vs. synthetic control
line treat month, lp(solid)lcolor(black) || line counterfact month, ///
lp(dash)lcolor(black) scheme(s1mono) xline(79) ylabel(0(0.25)0.5, angle(horizontal)) ///
title("A", position(10)) xtitle("Year") ytitle("Probability of Conflict") ///
legend(label(1 "DRC")) legend(label(2 "Synthetic DRC")) xlabel(0 "2004" 13 "2005" ///
25 "2006" 37 "2007" 49 "2008" 61 "2009" 73 "2010" 85 "2011" 97 "2012" 109 "2013" ///
121 "2014" 133 "2015" 145 "2016", angle(vertical))
graph save synth_civilians_A.gph, replace

* Plot the gap in predicted error
line gap month, lp(solid)lcolor(black) yline(0, lp(solid)lw(vthin)lcolor(black)) ///
xline(79, lp(solid)lw(vthin)lcolor(black)) xtitle("",si(medsmall)) xlabel(#10) ///
ytitle("Gap in Probability of Conflict", size(medsmall)) ///
xlabel(0 "2004" 13 "2005" 25 "2006" 37 "2007" 49 "2008" 61 "2009" 73 "2010" 85 ///
"2011" 97 "2012" 109 "2013" 121 "2014" 133 "2014" 145 "2016", angle(vertical)) ///
scheme(s1mono) title("B", position(10)) legend(off)
graph save synth_civilians_B.gph, replace

*** Battles
* Plot the gap in predicted error
use synth_battles.dta, clear
keep _Y_treated _Y_synthetic _time
drop if _time==.
rename _time month
rename _Y_treated  treat
rename _Y_synthetic counterfact
gen gap=treat-counterfact
sort month 

* Synthetic control effect estimates
egen postmean1 = mean(gap) if month>=79
egen premean1 = mean(gap) if month<79
egen postgap = mean(postmean1)
egen pregap = mean(premean1)
gen effect = postgap - pregap
sum effect

* Calculate RMSPE ratio
gen gap3=gap*gap
egen postmean=mean(gap3) if month>79
egen premean=mean(gap3) if month<=79
gen rmspe=sqrt(premean) if month<=79
replace rmspe=sqrt(postmean) if month>79
gen ratio=rmspe/rmspe[_n-1] if month==80
gen rmspe_post=sqrt(postmean) if month>79
gen rmspe_pre=rmspe[_n-1] if month==79

* Plot the treated vs. synthetic control
line treat month, lp(solid)lcolor(black) || line counterfact month, ///
lp(dash)lcolor(black) scheme(s1mono) xline(79) ylabel(0(0.25)0.5, angle(horizontal)) ///
title("A", position(10)) xtitle("Year") ytitle("Probability of Conflict") ///
legend(label(1 "DRC")) legend(label(2 "Synthetic DRC")) xlabel(0 "2004" 13 "2005" ///
25 "2006" 37 "2007" 49 "2008" 61 "2009" 73 "2010" 85 "2011" 97 "2012" 109 "2013" ///
121 "2014" 133 "2015" 145 "2016", angle(vertical))
graph save synth_battles_A.gph, replace

* Plot the gap in predicted error
line gap month, lp(solid)lcolor(black) yline(0, lp(solid)lw(vthin)lcolor(black)) ///
xline(79, lp(solid)lw(vthin)lcolor(black)) xtitle("",si(medsmall)) xlabel(#10) ///
ytitle("Gap in Probability of Conflict", size(medsmall)) ///
xlabel(0 "2004" 13 "2005" 25 "2006" 37 "2007" 49 "2008" 61 "2009" 73 "2010" 85 ///
"2011" 97 "2012" 109 "2013" 121 "2014" 133 "2015" 145 "2016", angle(vertical)) ///
scheme(s1mono) title("B", position(10)) legend(off)
graph save synth_battles_B.gph, replace

*** Riots and Protests
* Plot the gap in predicted error
use synth_riots_protest.dta, clear
keep _Y_treated _Y_synthetic _time
drop if _time==.
rename _time month
rename _Y_treated  treat
rename _Y_synthetic counterfact
gen gap=treat-counterfact
sort month

 * Synthetic control effect estimates
egen postmean1 = mean(gap) if month>=79
egen premean1 = mean(gap) if month<79
egen postgap = mean(postmean1)
egen pregap = mean(premean1)
gen effect = postgap - pregap
sum effect

* Calculate RMSPE ratio
gen gap3=gap*gap
egen postmean=mean(gap3) if month>79
egen premean=mean(gap3) if month<=79
gen rmspe=sqrt(premean) if month<=79
replace rmspe=sqrt(postmean) if month>79
gen ratio=rmspe/rmspe[_n-1] if month==80
gen rmspe_post=sqrt(postmean) if month>79
gen rmspe_pre=rmspe[_n-1] if month==79

* Plot the treated vs. synthetic control
line treat month, lp(solid)lcolor(black) || line counterfact month, ///
lp(dash)lcolor(black) scheme(s1mono) xline(79) ylabel(0(0.25)0.5, angle(horizontal)) ///
title("A", position(10)) xtitle("Year") ytitle("Probability of Conflict") ///
legend(label(1 "DRC")) legend(label(2 "Synthetic DRC")) xlabel(0 "2004" 13 "2005" ///
25 "2006" 37 "2007" 49 "2008" 61 "2009" 73 "2010" 85 "2011" 97 "2012" 109 "2013" ///
121 "2014" 133 "2015" 145 "2016", angle(vertical))
graph save synth_riots_protest_A.gph, replace

* Plot the gap in predicted error
line gap month, lp(solid)lcolor(black) yline(0, lp(solid)lw(vthin)lcolor(black)) ///
xline(79, lp(solid)lw(vthin)lcolor(black)) xtitle("",si(medsmall)) xlabel(#10) ///
ytitle("Gap in Probability of Conflict", size(medsmall)) ///
xlabel(0 "2004" 13 "2005" 25 "2006" 37 "2007" 49 "2008" 61 "2009" 73 "2010" 85 ///
"2011" 97 "2012" 109 "2013" 121 "2014" 133 "2015" 145 "2016", angle(vertical)) ///
scheme(s1mono) title("B", position(10)) legend(off)
graph save synth_riots_protest_B.gph, replace

*** Deadly conflict
* Plot the gap in predicted error
use synth_deadly_conflict.dta, clear
keep _Y_treated _Y_synthetic _time
drop if _time==.
rename _time month
rename _Y_treated  treat
rename _Y_synthetic counterfact
gen gap=treat-counterfact
sort month

 * Synthetic control effect estimates
egen postmean1 = mean(gap) if month>=79
egen premean1 = mean(gap) if month<79
egen postgap = mean(postmean1)
egen pregap = mean(premean1)
gen effect = postgap - pregap
sum effect

* Calculate RMSPE ratio
gen gap3=gap*gap
egen postmean=mean(gap3) if month>79
egen premean=mean(gap3) if month<=79
gen rmspe=sqrt(premean) if month<=79
replace rmspe=sqrt(postmean) if month>79
gen ratio=rmspe/rmspe[_n-1] if month==80
gen rmspe_post=sqrt(postmean) if month>79
gen rmspe_pre=rmspe[_n-1] if month==79

* Plot the treated vs. synthetic control
line treat month, lp(solid)lcolor(black) || line counterfact month, ///
lp(dash)lcolor(black) scheme(s1mono) xline(79) ylabel(0(0.25)0.5, angle(horizontal)) ///
title("A", position(10)) xtitle("Year") ytitle("Probability of Conflict") ///
legend(label(1 "DRC")) legend(label(2 "Synthetic DRC")) xlabel(0 "2004" 13 "2005" ///
25 "2006" 37 "2007" 49 "2008" 61 "2009" 73 "2010" 85 "2011" 97 "2012" 109 "2013" ///
121 "2014" 133 "2015" 145 "2016", angle(vertical))
graph save synth_deadly_A.gph, replace

* Plot the gap in predicted error
line gap month, lp(solid)lcolor(black) yline(0, lp(solid)lw(vthin)lcolor(black)) ///
xline(79, lp(solid)lw(vthin)lcolor(black)) xtitle("",si(medsmall)) xlabel(#10) ///
ytitle("Gap in Probability of Conflict", size(medsmall)) ///
xlabel(0 "2004" 13 "2005" 25 "2006" 37 "2007" 49 "2008" 61 "2009" 73 "2010" 85 ///
"2011" 97 "2012" 109 "2013" 121 "2014" 133 "2015" 145 "2016", angle(vertical)) ///
scheme(s1mono) title("B", position(10)) legend(off)
graph save synth_deadly_B.gph, replace

**** 
* Inferential test: Estimate the pre-/post-RMSPE and calculate the ratio 
****

** All conflict

local countrylist 1 2 3 4 5 7 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

foreach i of local countrylist {
	preserve
	use "conflict_panel.dta", clear

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_count==0
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0

	egen country_admin1 = concat(country admin1)

	* Define donor pool
	drop group

	keep if country=="Democratic Republic of Congo" | country=="Kenya" | country=="Ethiopia" | ///
	country=="Chad" | country=="Cameroon" | country=="Gabon" | country=="Mozambique" | ///
	country=="Malawi" | country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | ///
	country=="South Africa" | country=="Nigeria" | country=="Senegal" | country=="Guinea" | ///
	country=="Sierra Leone" | country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | ///
	country=="Gambia" | country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | ///
	country=="Burkina Faso" | country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
	country=="Swaziland" | country=="Niger" | country=="Mali"

	replace country="DRC" if country=="Democratic Republic of Congo"

	encode country_admin1, generate(geo1_id)
	label drop geo1_id
	tostring year_month, generate(year_month_str)
	drop year_month
	encode year_month_str, generate (year_month)
	encode country, generate(country_id)
	replace geo1_id = 400 if country_id==`i'

	collapse (mean) conflict_flag civilians_flag battles_flag other_flag ///
	deadly_conflict_flag, by(geo1_id year_month)

	tsset geo1_id year_month
	
	gen ma_conflict = (F5.conflict_flag + F4.conflict_flag + F3.conflict_flag + ///
	F2.conflict_flag + F1.conflict_flag + conflict_flag + L1.conflict_flag + ///
	L2.conflict_flag + L3.conflict_flag + L4.conflict_flag + L5.conflict_flag)/11
	
	drop if ma_conflict==.

	tsset geo1_id year_month

	synth ma_conflict ma_conflict, trunit(400) trperiod(79) keep(synth_allconflict_rmspe`i'.dta) replace
	
	use synth_allconflict_rmspe`i'.dta, clear
	keep _Y_treated _Y_synthetic _time
	drop if _time==.
	rename _time month
	rename _Y_treated  treat
	rename _Y_synthetic counterfact
	gen gap=treat-counterfact
	sort month 

	* Calculate RMSPE ratio
	gen gap3=gap*gap
	egen postmean=mean(gap3) if month>79
	egen premean=mean(gap3) if month<=79
	gen rmspe=sqrt(premean) if month<=79
	replace rmspe=sqrt(postmean) if month>79
	gen ratio=rmspe/rmspe[_n-1] if month==80
	gen rmspe_post=sqrt(postmean) if month>79
	gen rmspe_pre=rmspe[_n-1] if month==79

	mkmat rmspe_pre rmspe_post ratio if month==80, matrix (country`i')
	
	restore
}
*

foreach i of local countrylist {
	matrix rownames country`i'=`i'
	matlist country`i', names(rows)
}
*
mat country=country1\country2\country3\country4\country5\country6\country7\country8\country9\country10\country11\country12\country13\country14\country15\country16\country17\country18\country19\country20\country21\country22\country23\country24\country25\country26\country27\country28\country29\country30
mat2txt, matrix(country) saving(rmspe_bmprate.txt) replace
insheet using rmspe_bmprate.txt, clear
ren v1 country
drop v5
gsort -ratio
gen rank=_n

export excel using rmspe_bmprate, firstrow(variables) replace

import excel rmspe_bmprate.xls, sheet("Sheet1") firstrow clear
histogram ratio, bin(20) frequency fcolor(gs13) lcolor(black) ylabel(0(2)6) ///
scheme(s1mono) xtitle(Post/pre RMSPE ratio) xlabel(0(5)20) 

*** Violence against civilians

local countrylist 1 2 3 4 5 7 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

foreach i of local countrylist {
	preserve
	use "conflict_panel.dta", clear

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_count==0
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0

	egen country_admin1 = concat(country admin1)

	* Define donor pool
	drop group

	keep if country=="Democratic Republic of Congo" | country=="Kenya" | country=="Ethiopia" | ///
	country=="Chad" | country=="Cameroon" | country=="Gabon" | country=="Mozambique" | ///
	country=="Malawi" | country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | ///
	country=="South Africa" | country=="Nigeria" | country=="Senegal" | country=="Guinea" | ///
	country=="Sierra Leone" | country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | ///
	country=="Gambia" | country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | ///
	country=="Burkina Faso" | country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
	country=="Swaziland" | country=="Niger" | country=="Mali"

	replace country="DRC" if country=="Democratic Republic of Congo"

	encode country_admin1, generate(geo1_id)
	label drop geo1_id
	tostring year_month, generate(year_month_str)
	drop year_month
	encode year_month_str, generate (year_month)
	encode country, generate(country_id)
	replace geo1_id = 400 if country_id==`i'

	collapse (mean) conflict_flag civilians_flag battles_flag other_flag ///
	deadly_conflict_flag, by(geo1_id year_month)

	tsset geo1_id year_month
	
	gen ma_civilians = (F5.civilians_flag + F4.civilians_flag + F3.civilians_flag + ///
	F2.civilians_flag + F1.civilians_flag + civilians_flag + L1.civilians_flag + ///
	L2.civilians_flag + L3.civilians_flag + L4.civilians_flag + L5.civilians_flag)/11
	
	drop if ma_civilians==.

	tsset geo1_id year_month

	synth ma_civilians ma_civilians, trunit(400) trperiod(79) keep(synth_civilians_rmspe`i'.dta) replace
	
	use synth_civilians_rmspe`i'.dta, clear
	keep _Y_treated _Y_synthetic _time
	drop if _time==.
	rename _time month
	rename _Y_treated  treat
	rename _Y_synthetic counterfact
	gen gap=treat-counterfact
	sort month 

	* Calculate RMSPE ratio
	gen gap3=gap*gap
	egen postmean=mean(gap3) if month>79
	egen premean=mean(gap3) if month<=79
	gen rmspe=sqrt(premean) if month<=79
	replace rmspe=sqrt(postmean) if month>79
	gen ratio=rmspe/rmspe[_n-1] if month==80
	gen rmspe_post=sqrt(postmean) if month>79
	gen rmspe_pre=rmspe[_n-1] if month==79

	mkmat rmspe_pre rmspe_post ratio if month==80, matrix (country`i')
	
	restore
}
*

foreach i of local countrylist {
	matrix rownames country`i'=`i'
	matlist country`i', names(rows)
}
*
mat country=country1\country2\country3\country4\country5\country6\country7\country8\country9\country10\country11\country12\country13\country14\country15\country16\country17\country18\country19\country20\country21\country22\country23\country24\country25\country26\country27\country28\country29\country30
mat2txt, matrix(country) saving(civilians_rmspe_bmprate.txt) replace
insheet using civilians_rmspe_bmprate.txt, clear
ren v1 country
drop v5
gsort -ratio
gen rank=_n

export excel using civlians_rmspe_bmprate, firstrow(variables) replace

import excel civlians_rmspe_bmprate.xls, sheet("Sheet1") firstrow clear
histogram ratio, bin(20) frequency fcolor(gs13) lcolor(black) ylabel(0(2)6) ///
scheme(s1mono) xtitle(Post/pre RMSPE ratio) xlabel(0(5)20) 

*** Rebel group battles

local countrylist 1 2 3 4 5 7 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

foreach i of local countrylist {
	preserve
	use "conflict_panel.dta", clear

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_count==0
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0

	egen country_admin1 = concat(country admin1)

	* Define donor pool
	drop group

	keep if country=="Democratic Republic of Congo" | country=="Kenya" | country=="Ethiopia" | ///
	country=="Chad" | country=="Cameroon" | country=="Gabon" | country=="Mozambique" | ///
	country=="Malawi" | country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | ///
	country=="South Africa" | country=="Nigeria" | country=="Senegal" | country=="Guinea" | ///
	country=="Sierra Leone" | country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | ///
	country=="Gambia" | country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | ///
	country=="Burkina Faso" | country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
	country=="Swaziland" | country=="Niger" | country=="Mali"

	replace country="DRC" if country=="Democratic Republic of Congo"

	encode country_admin1, generate(geo1_id)
	label drop geo1_id
	tostring year_month, generate(year_month_str)
	drop year_month
	encode year_month_str, generate (year_month)
	encode country, generate(country_id)
	replace geo1_id = 400 if country_id==`i'

	collapse (mean) conflict_flag civilians_flag battles_flag other_flag ///
	deadly_conflict_flag, by(geo1_id year_month)

	tsset geo1_id year_month
	
	gen ma_battles = (F5.battles_flag + F4.battles_flag + F3.battles_flag + ///
	F2.battles_flag + F1.battles_flag + battles_flag + L1.battles_flag + ///
	L2.battles_flag + L3.battles_flag + L4.battles_flag + L5.battles_flag)/11
	
	drop if ma_battles==.

	tsset geo1_id year_month

	synth ma_battles ma_battles, trunit(400) trperiod(79) keep(synth_battles_rmspe`i'.dta) replace
	
	use synth_battles_rmspe`i'.dta, clear
	keep _Y_treated _Y_synthetic _time
	drop if _time==.
	rename _time month
	rename _Y_treated  treat
	rename _Y_synthetic counterfact
	gen gap=treat-counterfact
	sort month 

	* Calculate RMSPE ratio
	gen gap3=gap*gap
	egen postmean=mean(gap3) if month>79
	egen premean=mean(gap3) if month<=79
	gen rmspe=sqrt(premean) if month<=79
	replace rmspe=sqrt(postmean) if month>79
	gen ratio=rmspe/rmspe[_n-1] if month==80
	gen rmspe_post=sqrt(postmean) if month>79
	gen rmspe_pre=rmspe[_n-1] if month==79

	mkmat rmspe_pre rmspe_post ratio if month==80, matrix (country`i')
	
	restore
}
*

foreach i of local countrylist {
	matrix rownames country`i'=`i'
	matlist country`i', names(rows)
}
*
mat country=country1\country2\country3\country4\country5\country6\country7\country8\country9\country10\country11\country12\country13\country14\country15\country16\country17\country18\country19\country20\country21\country22\country23\country24\country25\country26\country27\country28\country29\country30
mat2txt, matrix(country) saving(battles_rmspe_bmprate.txt) replace
insheet using battles_rmspe_bmprate.txt, clear
ren v1 country
drop v5
gsort -ratio
gen rank=_n

export excel using battles_rmspe_bmprate, firstrow(variables) replace

import excel battles_rmspe_bmprate.xls, sheet("Sheet1") firstrow clear
histogram ratio, bin(20) frequency fcolor(gs13) lcolor(black) ylabel(0(2)6) ///
scheme(s1mono) xtitle(Post/pre RMSPE ratio) xlabel(0(5)20) 

*** Other

local countrylist 1 2 3 4 5 7 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

foreach i of local countrylist {
	preserve
	use "conflict_panel.dta", clear

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_count==0
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0

	egen country_admin1 = concat(country admin1)

	* Define donor pool
	drop group

	keep if country=="Democratic Republic of Congo" | country=="Kenya" | country=="Ethiopia" | ///
	country=="Chad" | country=="Cameroon" | country=="Gabon" | country=="Mozambique" | ///
	country=="Malawi" | country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | ///
	country=="South Africa" | country=="Nigeria" | country=="Senegal" | country=="Guinea" | ///
	country=="Sierra Leone" | country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | ///
	country=="Gambia" | country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | ///
	country=="Burkina Faso" | country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
	country=="Swaziland" | country=="Niger" | country=="Mali"

	replace country="DRC" if country=="Democratic Republic of Congo"

	encode country_admin1, generate(geo1_id)
	label drop geo1_id
	tostring year_month, generate(year_month_str)
	drop year_month
	encode year_month_str, generate (year_month)
	encode country, generate(country_id)
	replace geo1_id = 400 if country_id==`i'

	collapse (mean) conflict_flag civilians_flag battles_flag other_flag ///
	deadly_conflict_flag, by(geo1_id year_month)

	tsset geo1_id year_month
	
	gen ma_other = (F5.other_flag + F4.other_flag + F3.other_flag + ///
	F2.other_flag + F1.other_flag + other_flag + L1.other_flag + ///
	L2.other_flag + L3.other_flag + L4.other_flag + L5.other_flag)/11
	
	drop if ma_other==.

	tsset geo1_id year_month

	synth ma_other ma_other, trunit(400) trperiod(79) keep(synth_other_rmspe`i'.dta) replace
	
	use synth_other_rmspe`i'.dta, clear
	keep _Y_treated _Y_synthetic _time
	drop if _time==.
	rename _time month
	rename _Y_treated  treat
	rename _Y_synthetic counterfact
	gen gap=treat-counterfact
	sort month 

	* Calculate RMSPE ratio
	gen gap3=gap*gap
	egen postmean=mean(gap3) if month>79
	egen premean=mean(gap3) if month<=79
	gen rmspe=sqrt(premean) if month<=79
	replace rmspe=sqrt(postmean) if month>79
	gen ratio=rmspe/rmspe[_n-1] if month==80
	gen rmspe_post=sqrt(postmean) if month>79
	gen rmspe_pre=rmspe[_n-1] if month==79

	mkmat rmspe_pre rmspe_post ratio if month==80, matrix (country`i')
	
	restore
}
*

foreach i of local countrylist {
	matrix rownames country`i'=`i'
	matlist country`i', names(rows)
}
*
mat country=country1\country2\country3\country4\country5\country6\country7\country8\country9\country10\country11\country12\country13\country14\country15\country16\country17\country18\country19\country20\country21\country22\country23\country24\country25\country26\country27\country28\country29\country30
mat2txt, matrix(country) saving(other_rmspe_bmprate.txt) replace
insheet using other_rmspe_bmprate.txt, clear
ren v1 country
drop v5
gsort -ratio
gen rank=_n

export excel using other_rmspe_bmprate, firstrow(variables) replace

import excel other_rmspe_bmprate.xls, sheet("Sheet1") firstrow clear
histogram ratio, bin(20) frequency fcolor(gs13) lcolor(black) ylabel(0(2)6) ///
scheme(s1mono) xtitle(Post/pre RMSPE ratio) xlabel(0(5)20) 

*** Deadly conflict

local countrylist 1 2 3 4 5 7 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

foreach i of local countrylist {
	preserve
	use "conflict_panel.dta", clear

	drop year_month
	gen year_month = string(year, "%04.0f") + string(month, "%02.0f")
	destring year_month, replace

	gen conflict_flag=1 if conflict_count>0
	replace conflict_flag=0 if conflict_count==0
	gen civilians_flag=1 if civilians>0
	replace civilians_flag=0 if civilians==0
	gen battles_flag=1 if battles>0
	replace battles_flag=0 if battles==0
	gen other_flag=1 if other>0
	replace other_flag=0 if other==0
	gen deadly_conflict_flag=1 if fatalities>0
	replace deadly_conflict_flag=0 if fatalities==0

	egen country_admin1 = concat(country admin1)

	* Define donor pool
	drop group

	keep if country=="Democratic Republic of Congo" | country=="Kenya" | country=="Ethiopia" | ///
	country=="Chad" | country=="Cameroon" | country=="Gabon" | country=="Mozambique" | ///
	country=="Malawi" | country=="Botswana" | country=="Namibia" | country=="Zimbabwe" | ///
	country=="South Africa" | country=="Nigeria" | country=="Senegal" | country=="Guinea" | ///
	country=="Sierra Leone" | country=="Liberia" | country=="Ivory Coast" | country=="Ghana" | ///
	country=="Gambia" | country=="Guinea-Bissau" | country=="Togo" | country=="Benin" | ///
	country=="Burkina Faso" | country=="Eritrea" | country=="Djibouti" | country=="Lesotho" | ///
	country=="Swaziland" | country=="Niger" | country=="Mali"

	replace country="DRC" if country=="Democratic Republic of Congo"

	encode country_admin1, generate(geo1_id)
	label drop geo1_id
	tostring year_month, generate(year_month_str)
	drop year_month
	encode year_month_str, generate (year_month)
	encode country, generate(country_id)
	replace geo1_id = 400 if country_id==`i'

	collapse (mean) conflict_flag civilians_flag battles_flag other_flag ///
	deadly_conflict_flag, by(geo1_id year_month)

	tsset geo1_id year_month
	
	gen ma_deadly = (F5.deadly_conflict_flag + F4.deadly_conflict_flag + F3.deadly_conflict_flag + ///
	F2.deadly_conflict_flag + F1.deadly_conflict_flag + deadly_conflict_flag + L1.deadly_conflict_flag + ///
	L2.deadly_conflict_flag + L3.deadly_conflict_flag + L4.deadly_conflict_flag + L5.deadly_conflict_flag)/11
	
	drop if ma_deadly==.

	tsset geo1_id year_month

	synth ma_deadly ma_deadly, trunit(400) trperiod(79) keep(synth_deadly_rmspe`i'.dta) replace
	
	use synth_deadly_rmspe`i'.dta, clear
	keep _Y_treated _Y_synthetic _time
	drop if _time==.
	rename _time month
	rename _Y_treated  treat
	rename _Y_synthetic counterfact
	gen gap=treat-counterfact
	sort month 

	* Calculate RMSPE ratio
	gen gap3=gap*gap
	egen postmean=mean(gap3) if month>79
	egen premean=mean(gap3) if month<=79
	gen rmspe=sqrt(premean) if month<=79
	replace rmspe=sqrt(postmean) if month>79
	gen ratio=rmspe/rmspe[_n-1] if month==80
	gen rmspe_post=sqrt(postmean) if month>79
	gen rmspe_pre=rmspe[_n-1] if month==79

	mkmat rmspe_pre rmspe_post ratio if month==80, matrix (country`i')
	
	restore
}
*

foreach i of local countrylist {
	matrix rownames country`i'=`i'
	matlist country`i', names(rows)
}
*
mat country=country1\country2\country3\country4\country5\country6\country7\country8\country9\country10\country11\country12\country13\country14\country15\country16\country17\country18\country19\country20\country21\country22\country23\country24\country25\country26\country27\country28\country29\country30
mat2txt, matrix(country) saving(deadly_rmspe_bmprate.txt) replace
insheet using deadly_rmspe_bmprate.txt, clear
ren v1 country
drop v5
gsort -ratio
gen rank=_n

export excel using deadly_rmspe_bmprate, firstrow(variables) replace

import excel deadly_rmspe_bmprate.xls, sheet("Sheet1") firstrow clear
histogram ratio, bin(20) frequency fcolor(gs13) lcolor(black) ylabel(0(2)6) ///
scheme(s1mono) xtitle(Post/pre RMSPE ratio) xlabel(0(5)20) 


*** Correlation between conflict and mining exports

** Not aggregated to the country level
* DRC only panel
use "conflict_panel.dta", clear
drop group _merge
keep if country=="Democratic Republic of Congo"
rename year (Year)

merge m:1 Year using "mineral_exports.dta"

drop if _merge==1

gen conflict_flag = 1 if conflict_count>0
replace conflict_flag = 0 if conflict_count==0

reg conflict_flag Tin, robust
outreg2  using "TableY.tex" , keep(Tin) replace
reg conflict_flag Tantalum, robust
outreg2  using "TableY.tex" , keep(Tantalum)
reg conflict_flag Tungsten, robust
outreg2  using "TableY.tex" , keep(Tungsten)
reg conflict_flag Gold, robust
outreg2  using "TableY.tex" , keep(Gold)

** Aggregated to the country level 
* Load Raw ACLED data
use "ACLED_All_Africa.dta", clear
split event_date, p("/")
egen month_year = concat( event_date2 event_date3) 

gen admin2_n = 1
gen civilians = 1 if event_type=="Violence against civilians"
gen battles = 1 if event_type=="Battle-Government regains territory" | ///
event_type=="Battle-No change of territory" | event_type=="Battle-Non-state actor overtakes territory" 
gen riots_protest = event_type =="Riots/Protests"
gen other = 1 if event_type=="Headquarters or base established" | ///
event_type=="Non-violent transfer of territory" | event_type=="Remote violence" | ///
event_type=="Strategic development"
collapse (sum) admin2_n civilians battles riots_protest other fatalities, by(country year)

keep if country=="Democratic Republic of Congo"

rename year (Year)
drop if Year>"2015"
drop if Year<"2004"
destring Year, gen(Year1)
drop Year
rename Year1 (Year)

merge 1:1 Year using "mineral_exports.dta"

rename admin2_n (any_conflict)

reg any_conflict Tin, robust
outreg2  using "TableZ.tex" , keep(Tin) replace
reg any_conflict Tantalum, robust
outreg2  using "TableZ.tex" , keep(Tantalum)
reg any_conflict Tungsten, robust
outreg2  using "TableZ.tex" , keep(Tungsten)
reg any_conflict Gold, robust
outreg2  using "TableZ.tex" , keep(Gold)

